Rajeev,
Here is a complete working code which explains the details.
Courtesy : Jim Gibson and Shawn H Corey for the detailed explanation
[code]
use strict;
use warnings;
my @data = qw/
"apples"
oranges
"guavas"
cherri"es
mangoes
/;
print join ',', map { $_ =~ s/\"/\'/g; "\"$_\"" } @data;
#Explanation:-
#
#The map builtin, takes a list and returns a new list.
#It maps old values to new values.
#
#Its syntax is
#
#NEWLIST = map BLOCK LIST;
#NEWLIST = map EXPRESSION, LIST;
#
#map operates on every element in a list, so use it only if you
#want to transform an entire list.
#$_=~ s/\"/\'/g; changes every double quotes to single quotes and
#because you are operating on $_. It changes the original array @data.
#Note that $_ is an alias and not a copy. So any changes you do in $_
#will affect the original array. In this case @data.
#Result of running the first statement
#'apples' oranges 'guavas' cherri'es mangoes
#"\"$_\"" makes all the values enclosed in double quotes. This will not
#affect the original array
#Result of running the second statement
#"'apples'" "oranges" "'guavas'" "cherri'es" "mangoes"
#Result of running the entire statement (Note the output contains ',' as
#a result of join)
#"'apples'","oranges","'guavas'","cherri'es","mangoes"
[/output]
best,
Shaji
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------
________________________________
From: Rajeev Kilaru <[email protected]>
To: [email protected]
Sent: Thursday, 25 July 2013 12:14 AM
Subject: Unable to understanding map usage
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 )
Thanks,
Rajeev Kilaru
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/