Update: This prints the line for each word only once. If you instead want to print each line only once, regardless of how many words it matches, try this:#!/usr/bin/perl use warnings; print "Search for what: "; chomp(my $search = <STDIN>); my %found; #used as a set later @words = split(/ /, $search); open(DATA, "books.txt") or die "error opening file $!"; while (<DATA>) { foreach $word(@words) { chomp($word); if (/$word/i) { print unless defined($found{lc($word)}); $found{lc($word)}=1; #indicates that word has been found } } }
#!/usr/bin/perl use warnings; print "Search for what: "; chomp(my $search = <STDIN>); @words = split(/ /, $search); open(DATA, "books.txt") or die "error opening file $!"; while (<DATA>) { foreach $word(@words) { chomp($word); if (/$word/i) { print; last; #skips checking this line against oth +er words } } }
In reply to RE: Re: Trying to make a search Part 2
by swiftone
in thread Trying to make a search Part 2
by zdog
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |