foreach $key( map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { m/q(\d+)/; [ $1, $_ ] } keys %hash ){ print "$key : $hash{$key}\n"; }