As many here have shown, map is a great (and probably the best) way to get what you are looking for... It is a handy tool, but, it can be a little on the arcane side if you are not used to it. Another option is foreach. If map makes your brain hurt (it does mine sometimes), foreach may be your answer. I use it most often for looping over lists (arrays/hashes/refs of both, etc...). And, if you use labels, you get some pretty easy loop control. Here is the example foreach:

my @new_array; ELEMENT: foreach my $href (@{$aref}) { push @new_array, $href if (exists $href->{name}); }

Yes, it seems like more coding than map, and it is... But, it tends to be ultra readable and really obvious about what it is doing. And like @1nickt, I sometimes lean to the more obvious coding approach, so the next poor coder to come along in 6 months does not have to spend the weekend trying to reverse engineer my cleverness (btw, that poor coder is usually me).


In reply to Re: Extracting elements from array by shilo
in thread Extracting elements from array 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.