Answering your second question, you have two syntax errors in the provided code. First, by including my in your assignment statement, you are masking the $value variable declared in your while statement (This will get caught if you use strict;use warnings). Second, you are overwriting your %copy hash on every iteration with the new key-value pair. What you mean is likely:
use strict;
use warnings;
my %copy = ();
while ( my ($key, $value) = each(%hash) ) {
$copy{$value} = $key;
}
This can be done in one line as:
my %copy = reverse %hash;
Be aware that this will not behave well if you have repeat values.
Update: This is addressed in perlfaq4.
|