Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: What I Most Recently Learned in Perl, But Should Have Already Known

by arpie (Initiate)
on Aug 22, 2006 at 18:08 UTC ( #568934=note: print w/ replies, xml ) Need Help??


in reply to What I Most Recently Learned in Perl, But Should Have Already Known

Map and grep. Especially for set functions, e.g. (untested code, but that should give the idea)

my @a=(1,2,3,4,5); my @b=(4,5,6,7,8); my %a = map { $_ => 1 } @a; my %b = map { $_ => 1 } @b; my @intersection = grep { exists $a{$_} } @b; my @intersection_complement = grep { !exists $a{$_} } @b;


Comment on Re: What I Most Recently Learned in Perl, But Should Have Already Known
Download Code
Re^2: What I Most Recently Learned in Perl, But Should Have Already Known
by pKai (Priest) on Aug 23, 2006 at 08:49 UTC

    That reminds me that some time back I tried to get a grip on hash slices.

    In similar use to your example:

    my @intersection = grep {defined} @{{map {$_=>$_} @a}}{@b}

    Though in everyday life I prefer your method, since I tend to get headache from thinking about that slices and also definitely got the syntax and/or semantics wrong the first time always.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (17)
As of 2015-07-02 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (44 votes), past polls