in reply to (zdog) Re: Trying to make a search Part 2
in thread Trying to make a search Part 2

But now how would I get it to only print out the entry once even when there are multiple matches
Like so:
#!/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 } } }
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>); @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 } } }