in reply to How to use 'last' / 'next' commands to exit loop?
I define a hash table and an array upfront. Then, I want to read each element of the array, find if a 'key' exists in the hash table. If found, push the corresponding 'value' into a new array, if not, push the original element (read from the array) into the new array.
my @new=map { exists $hash{$_} ? $hash{$_} : $_ } @array;
If the values of %hash are guaranteed not to be false, then also
my @new=map $hash{$_} || $_, @array;
(I prefer the comma form with short expressions and the block form for longer ones, not to mention those cases in which you have more statements and you are "forced" to use a block, or else a do, but... why?)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to use 'last' / 'next' commands to exit loop?
by sara2005 (Scribe) on May 30, 2006 at 17:52 UTC |