foreach $key (sort { $b <=> $a } keys %method) { $message_ref->[0] =~ s/$key/$method{$key}/; }