http://www.perlmonks.org?node_id=501441


in reply to question for perl book & magazine authors

I'm convinced that map & grep aren't as heavily used as they could be because people don't understand them.
And because (some) people don't understand them, it's also (sometimes) hard for people to use them correctly and tell at a glance what is going on. Take, for instance, your 'using grep' example. It doesn't work (also, you are using 'eq' in one vs. '==' in the other which may make an unfair benchmark). I assume you are finding common elements but eliminating any duplicates from the output. The grep example isn't eliminating items that have already been seen because it's not keeping track of what's been seen. I think you were aiming more for this (though I assume the O(n**2) algorithm is on purpose just for demonstration purposes):
'using grep' => sub { my @common; my %seen; @common = grep { my $element1 = $_; !$seen{$element1}++ and grep { $element1 eq $_ } @array2 } @array1; },
Update: and after the fix, the benchmark doesn't favor grep nearly as much.

Replies are listed 'Best First'.
Re^2: question for perl book & magazine authors
by jfroebe (Parson) on Oct 20, 2005 at 03:01 UTC
    dang typos of mine!

    Thanks! :)

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1