For a quick, but possibly wrong solution, you could use a hash slice:
my %newhash; @newhash{@array} = @oldhash{@array};
A pitfall here is that if there's a value in the array that doesn't correspond to a key in the original hash, you'll autovivify an entry.
If you don't have such control over your data, try:
my %new = map { $_=> $old{$_} } grep { defined $old{$_} } @array;
The grep gets all the entries in the hash that (a) have a key corresponding to an entry in the array and (b) have defined values. The map turns that list into a hash.
Change that defined to exists if you don't mind getting back keys associated with undefined values.
HTH!
perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
In reply to Re: Mapping array over a hash
by arturo
in thread Mapping array over a hash
by agoth
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |