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?)
In reply to Re: How to use 'last' / 'next' commands to exit loop?
by blazar
in thread How to use 'last' / 'next' commands to exit loop?
by sara2005
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |