sub bylen { length($b)<=>length($a)} # sorts by decreasing length of string foreach (sort keys bylen %trans) { s/$_/$trans{$_}/g; }