On Jul 24, 2013, at 11:44 AM, Rajeev Kilaru wrote:
> Hello,
>
> I am looking at the following code someone wrote and I have
> difficultly in understand map usage. Can somebody please explain how
> does the following code work?
>
> print OUT2 join( ',', map { $_=~ s/\"/\'/g; "\"$_\"" } @data )
Here is the documentation for map from 'perldoc -f map':
map BLOCK LIST
map EXPR,LIST
Evaluates the BLOCK or EXPR for each element of LIST (locally
setting $_ to each element) and returns the list value composed
of the results of each such evaluation.
In your case, BLOCK is
{ $_=~ s/\"/\'/g; "\"$_\"" }
and LIST is @data. Therefore, each element of @data is aliased to $_, the
substitution s//\"/\'/g is performed on it, which changes all double-quote
characters to single-quote apostrophes, Then, the second 'line' of the BLOCK is
evaluated and pushed onto the result list, which puts the transformed string
between double-quotes. The resulting list is joined with '.' characters and
printed to the OUT2 file stream.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/