First, what mtmcc said.
Second, a quote from the illustrious prophet: Doing linear scans over an associative array is like trying to club someone to death with a loaded Uzi. -- TimToady
You should put your keys into a hash, yes, but then just iterate over your array. The array values are exactly what you need to access the hash values. So it might look like:
If I were going to actually write this, I'd take advantage of the fact that the loop iterator for Foreach Loops is an lvalue for the array element ($_ = $id{$_} for @replaces;), but that might be a little to magical for your taste given your familiarity with the language.my %id = (bananas => 456, oranges => 23, peaches => 897236, kiwis => 3726, ); my @replaces = ('kiwis','oranges','bananas','bananas'); for my $i (0 .. $#replaces) { $replaces[$i] = $id{$replaces[$i]}; }
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
In reply to Re: using hashes
by kennethk
in thread iterating hash keys?
by R56
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |