in reply to Interchanging hash values
Also, you can swap the values of two variables without a temporary variable in the middle:$sort[scalar @sort - 1] == $sort[-1];
So your code can be rewritten like so:($a, $b) = ($b, $a); # swap the values of $a and $b
#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash = (Matt => 24, Dana => 19, Eric => 28, Sara => 20, John => 17, Mike => 23,); print Dumper \%hash; my @sort = sort {$hash{$a} <=> $hash{$b}} keys %hash; ($hash{$sort[0]}, $hash{$sort[-1]}) = ($hash{$sort[-1]}, $hash{$sort[0]}); print Dumper \%hash;
-Matt
|
---|