# I have already read the 2-column table file into %table. # I've also already opened INFILE and OUTFILE my @keys_ordered = sort { length $table_ref->{$b} <=> length $table_ref->{$a} } keys %{$table_ref}; # Sorted in reverse order of length, so that longer strings # in the data file are found before shorter strings # Done here to increase speed, vs inside the while loop my $replacecount = 0; while ( my $line = ) { for my $key ( @keys_ordered ) { $replacecount += ( $line =~ s/$key/$table_ref->{$key}/g ); } print OUTFILE $line; } print "Made $replacecount replacements.\n";