$pattern = join "|", @sorted; $text =~ s/($pattern)/$hash{$1}/g;