for (@sorted) { my $v = delete $hash{$_}; s/ /_/g; $hash{$_} = $v; }