map applies an expression to each element in a list, creating a new list:

my @files_and_sizes = map { [ $_, -s $_ ] } @filenames;

Here, we have an array of filenames. We use map to iterate over each element, creating an anonymous array consisting of the filename and the size of the file -- for each filename. Those end up in the array on the left hand side. (Read map statements left to right.)

There's nothing that says that the map block has to return anything related to the source list:

my @useless = map { 1 } qw( this is a silly test ); print "@useless\n";

grep applies an expression to each element in the list, returning a list of those elements for which the expression evaluated to be true.

my @set1 = qw (apples oranges pears mangoes); my @set2 = qw (bananas mangoes papayas coconuts); my %first_set; @first_set{@set1} = (); my @union = grep { exists $first_set{$_} } @set2;
We create two sets, then create a hash with keys from @set1. Our grep statement checks to see if each element returns a true from the block -- that is, it checks to see if each element of @set2 is a valid key in the hash. It returns a list of those keys (only 'mangoes').

In reply to Re: Map and Grep by chromatic
in thread Map and Grep by Anonymous Monk

Title:
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, details, 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, summary, 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.