> #Mode Idiomatic Way and is more efficient too!
> map { print "Take working vacation bud\n"; } grep{$_ eq 'george'}
> @{$hash{'jmj'}};
>
> I would prefer the second way -- the idiomatic way of doing it in Perl. I
> have read, somewhere that the second approach is more efficient.

Actually discarding the return value of map or grep is the subject of many
heated flamewars on usenet.

See this entry in the perlfaq about map and grep in a void context:
http://language.perl.com/newdocs/pod/perlfaq6.html#What_s_wrong_with_using_grep_or_

Personally I only use map when I actually care about the list that results
from the map, and for otherwise.  Here's a benchmark example:

use Benchmark;

my @testary1 = (0..100000);
my @testary2 = (0..100000);

my $mapcode = sub {

        map { $_++ } @testary1

};

my $forcode = sub {

        for (@testary2)
        {
                $_++
        }
};

timethis(100, $mapcode);
timethis(100, $forcode);

results:

timethis 100: 27 wallclock secs (26.00 usr +  0.03 sys = 26.03 CPU) @
3.84/s (n=100)
timethis 100: 11 wallclock secs (10.51 usr +  0.00 sys = 10.51 CPU) @
9.51/s (n=100)

It appears that for is faster for this limited example on my machine at
this moment with the planets aligned as they are currently.  Your mileage
may vary :-)

Luke


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to