in reply to Re: comparing array elements to hash keys
in thread comparing array elements to hash keys
I was going to suggest:
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.my @ary2 = grep {defined} @hash{@ary};
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;
|
---|
In Section
Seekers of Perl Wisdom