IN: while (<$INPUT>) { my @words = split /\b/, $_; foreach my $word (@words) { print $_ and next IN if exists $name_hash{$word}; } }