use strict; use warnings; my %words; for my $fnum ( 1 .. 4 ) { open( I, '<', "File$fnum.txt" ) or die "File$fnum.txt: $!\n"; while () { chomp; s/^[\s\d.]+//; # remove initial digit(s), period, whitespace, if any $words{$_} .= $fnum unless ( exists( $words{$_} ) and $words{$_} =~ /$fnum$/ ); } } print "Words found in all four input files:\n"; for ( sort keys %words ) { print "$_\n" if ( $words{$_} eq '1234' ); }