my %kwhash = (word1=> undef, word2=>undef); # SPLIT your line of input, then match each word while (my $line = ){ for my $word (split / /, $line){ if (defined $kwhash{$word}){ # Process for FOUND keyword } }