open my $fh, '<', $file1 or die $!; my %replace = map { chomp; split /-/, $_, 2 } <$fh>; my $re = join '|', sort { length $b <=> length $a } keys %replace; close $fh; open my $in, '<', $file2 or die $!; open my $out, '>', $file3 or die $!; while (my $line = <$in>) {  $line =~ s/\b($re)\b/$replace{$1}/g;  print $out $line; }