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

Re: Re: comparing array elements to hash keys

by dvergin (Monsignor)
on Jul 03, 2003 at 19:53 UTC ( #271273=note: print w/ replies, xml ) Need Help??


in reply to Re: comparing array elements to hash keys
in thread comparing array elements to hash keys

I was going to suggest:

my @ary2 = grep {defined} @hash{@ary};
It's tempting to just say   my @ary2 = @hash{@ary}.   But the values in @ary that don't match keys in %hash produce undefined values that have to be weeded out.

Here's a quick demo:

#!/usr/bin/perl -w use strict; my @ary = ('a', 'c', 'g', 'l'); my %hash = (a => 1, b => 2, c => 3, d => 4); my @ary2 = grep {defined} @hash{@ary}; print "[$_]\n" for @ary2;


Comment on Re: Re: comparing array elements to hash keys
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2015-07-29 03:26 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 (260 votes), past polls