in reply to Re^2: Map Vs Foreach
in thread Map Vs Foreach
is not the way to use map to generate a new list of @data with values of +2. here is the right way...my @arr1; @arr1 = map { push @arr1, ($_+ 2) } @data;
map{} returns the value of the last statement. I put this $bullshit statement in there to "throw you off"...it means absolutely nothing!my @dataPlus2 = map { my $bullshit = 'XYZ'; #see below $_ + 2 }@data;
Use the power of the language.
my @dataPlus2 = map { $_ + 2 }@data;
my @roots = map { sqrt($_) }@data;
map{} is best used for a line or two translations.
I wouldn't normally do it this way, this is just to show you that it is possible:
foreach my $root (map { sqrt($_) }@data) { #do something with this square root... }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Map Vs Foreach
by JadeNB (Chaplain) on Nov 28, 2009 at 05:29 UTC |