use strict; my %Counter; open( K, "input" ) or die "input (keyword file): $!"; while ( ) { chomp; $Counter{$k} = []; # initialize to (a reference to) an empty array } close K; # concatenate the keywords with "|" to form a regular expression: my $key_regex = join( '|', keys %Counter ); open( J, "solution.java" ) or die "solution.java: $!"; while ( ) { while ( /\b($key_regex)\b/g ) { push @{$Counter{$1}}, $.; } } close J; # now print a list of keyword hits: for my $key ( sort keys %Counter ) { next unless scalar @{$Counter{$k}}; print "$key found on lines @{$Counter{$k}}\n"; }