Can I use map instead of the foreach in this bit of code? i have two lists. One has the keys (@keys) I'd like to look for in the other list (@records). The records that have the key are passed through to a @result list. If the result list is non-empty, it goes to the final list as the @result list may be overwritten in the next iteration. Or, is there a totally better alforithm to do this? I have googled and looked on this site before posting but nothing stood out and hit me in the face but I'm sure a more elegant perl way of doing it exists ;)
my @keys = ( qw|a e i o u| ); my @records = (); push @records, '1|2|3|d|4'; push @records, '1|2|3|a|4'; push @records, '1|2|3|d|4'; push @records, '1|2|3|o|4'; my @final = (); my @result = (); my $re = ''; foreach my $key ( @keys ) { @result = grep{ m/^\d\|\d\|\d\|$key\|\d$/ } @records; push @final, [ @result ] if @result; } print map{ "@{ $final[$_] }\n" } 0..$#final;
Merci bien, Perl Monkers!!

In reply to Using map and grep to Sort one list using another list by AntsPants

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.