my %input; $input{$_} = undef for @$inputs; # make this a hash $/ = undef; # look up $/ in perldoc perlvar unless ( open( FILE, ...)) { #blah next; } $text = ; # $text holds entire file content. $text =~ s/\n\n/ <P> /; # (optional: preserve paragraph boundaries) @words = split( /\s+/, $text ); for my $i (0..$#words) { next unless ( exists( $input{$word[$i]} )); # get here when there's a match, output matched # word along with "N" words of surrounding context # (left as an exercise...) }