There are a couple of ways you can improve on this. First, you can access the last element of an array by using the index -1. I.e.
$sort[scalar @sort - 1] == $sort[-1];
Also, you can swap the values of two variables without a temporary variable in the middle:
($a, $b) = ($b, $a); # swap the values of $a and $b
So your code can be rewritten like so:
#!/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;


In reply to Re: Interchanging hash values by DrManhattan
in thread Interchanging hash values by mjab

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":