Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
"be consistent"
 
PerlMonks  

Re: Using map and grep to Sort one list using another list

by Moron (Curate)
on Feb 05, 2007 at 13:43 UTC ( #598295=note: print w/ replies, xml ) Need Help??


in reply to Using map and grep to Sort one list using another list

Do you mean like this?

my @result = map { my $key = $_; grep m/^\d\|\d\|\d\|$key\|\d$/, @records } @keys +;

-M

Free your mind


Comment on Re: Using map and grep to Sort one list using another list
Download Code
Re^2: Using map and grep to Sort one list using another list
by AntsPants (Novice) on Feb 05, 2007 at 13:52 UTC
    that is what I meant and that is what I tried in testing. see reply to first post ;) As I said, I feel an idiot!!
Re^2: Using map and grep to Sort one list using another list
by shmem (Canon) on Feb 05, 2007 at 14:05 UTC
    You will have undefined values in your @result array.

    the above is plain wrong--

    --shmem

      I don't see that happening. Perhaps I have missed something.

      use strict; use warnings; use Data::Dumper; my @keys = qw{a e i o u}; my @records = qw{ 1|2|3|d|4 1|2|3|a|4 1|2|3|d|4 1|2|3|o|4}; my @result = map { my $key = $_; grep { m{^(?:\d\|){3}$key\|\d$} } @records } @keys; my $dd = Data::Dumper->new([\@result], [qw{*result}]); print $dd->Dumpxs();

      produces

      @result = ( '1|2|3|a|4', '1|2|3|o|4' );

      Cheers,

      JohnGG

      My take on this was rather that when the grep fails to produce a match, it returns an empty list to map which then operates on no extra entries - i.e. doing nothing rather than constructing an undefined value.

      -M

      Free your mind

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://598295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2014-04-21 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls