#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash = ( Matt => 24, Dana => 19, Eric => 28, Sara => 20, John => 17, Mike => 23, ); my @sorted = sort{ $hash{$a} <=> $hash{$b} } keys %hash; @hash{ @sorted[0,-1] } = @hash{ @sorted[-1,0] }; print Dumper \%hash;