sub findtext { my ($fileargs, $inputs) = @_; # pass references to arrays my @filenames; for my $arg ( @$fileargs ) { # dereference this array push @filenames, grep /\w/, split( /\W+/, $arg ); } for my $file ( @filenames ) { unless open( FILE, "/home/jroberts/$file.txt" ) { warn "open failed on $file: $!"; next; } while () { for my $term ( @$inputs ) { # dereference this array next unless ( /\b$term\b/ ); # get here when there's a match... # (not sure what you want to do here) } } } } #### 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...) }