Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: question for perl book & magazine authors

by runrig (Abbot)
on Oct 19, 2005 at 21:48 UTC ( #501441=note: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://501441]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2018-06-21 20:54 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (119 votes). Check out past polls.