in reply to Mapping array over a hash
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"'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Mapping array over a hash
by japhy (Canon) on Jul 20, 2001 at 18:38 UTC | |
by larryk (Friar) on Jul 20, 2001 at 19:29 UTC | |
by japhy (Canon) on Jul 20, 2001 at 19:46 UTC |