私は、

>foreach $val (sort {$b <=> $a} values %hash){

の部分ではなく、

>if($val == %hash{$ky}){push @hashkey, $ky;}

の部分に対してレスしたまで、とむきになってみるテスト。