http://qs321.pair.com?node_id=573395


in reply to Finding problem with finding keywords in arrays / files

This probably isn't THE most efficient way to do this but it does what you are trying to achieve.

#!/usr/local/bin/perl -w use strict; open(DATA, "data.txt") or die "Error opening 'data.txt' $!\n"; my @data = <DATA>; open(KEYWORDS, '<', "keyword.txt") or die "Error opening 'keyword.txt' + $!\n"; my @keywd = <KEYWORDS>; close DATA; close KEYWORDS; foreach my $data ( @data ) { chomp($data); foreach my $keyword (@keywd) { chomp($keyword); if ( $data =~ /$keyword/i ) { print "Match \$data = $data and \$keyword = $keyword\n"; } } }

Replies are listed 'Best First'.
Re^2: Finding problem with finding keywords in arrays / files
by Anonymous Monk on Sep 17, 2006 at 10:59 UTC
    chomp(my @data = do { open my $f, "data.txt" or die "Error opening ``data.txt'': $!"; <$f> });
Re^2: Finding problem with finding keywords in arrays / files
by akirostar (Novice) on Sep 17, 2006 at 13:26 UTC
    Hi core, I tried what you have written, with regards to the IF loop, the program will enter it no matter how, is it possible to jus t enter into the loop during the actual matching? thanks.