# UNTESTED! use strict; use warnings; open( ABB, '<', 'jabb.txt' ) or die "Couldn't open ABB.\n$!"; open( IN, '<', 'input.txt' ) or die "Couldn't open infile.\n$!"; open( OUT, '>', 'output.out' ) or die "Couldn't open outfile.\n$!"; # prepare abreviations my %abb; while(){ # there are shorter ways to do it, but to keep your code... if(/(.*?)\t(.*?)\n/) { $abb{$2}= $1 } } close ABB; # build a RE for quickly(?) finding abbreviatable words. my $abbre= join('|', map "\Q$_\E",keys %abb); $abbre= qr/\b($abbre)\b/; # replace my @myin = ; while(){ s/$abbre/$abb{$1}/g; print OUT; } close IN; close OUT;