Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I've been reading a book on DBMS administration using perl (specific book doesn't matter) and have seen this in other perl books as well. (I'm not singling out any author)

Several books tend to avoid the use of map and grep. I can only assume that the authors have trouble explaining the usage and benefits of these in such a way that perl programmers can understand. How can we explain map and grep better?

One of the benefits is that often, using map or grep provides increased performance (see example below). Another is that you can chain the greps and maps together to make your life so much easier.

I'm convinced that map & grep aren't as heavily used as they could be because people don't understand them.


#!/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); ## benchmark to determine best method ## of finding common elements in two ## arrays my @array1 = ( '0', '1', '2', '3', '5' ); my @array2 = ( '0', '1', '2', '3', '4', '6' ); cmpthese (100000, { 'using foreach' => sub { my @common; foreach my $element1 (@array1) { foreach my $element2 (@array2) { if ($element1 eq $element2) { push @common, $element1 unless grep { $element +1 eq $_ } @common; } } } }, 'using grep' => sub { my @common; @common = grep { my $element1 = $_; ! grep { $element1 == $_ } @array2 } @array2; } });


$ ./test_bm_array Rate using foreach using grep using foreach 6873/s -- -30% using grep 9766/s 42% --

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

In reply to question for perl book & magazine authors by jfroebe

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [marioroy]: Added MCE::Inbox demonstration to Re^3: Hobo with a bit of recursion. I need to include MCE::Inbox in the next MCE::Shared release. That was fun.

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

      Results (124 votes). Check out past polls.