Bman70 has asked for the wisdom of the Perl Monks concerning the following question:
I want to check if an array of single words (strings) contains several specific words, in any order.
I have this code, cobbled together from advice found online:
my %hash = @allwords; #puts array in hash to search / compare. if (exists $hash{'word1' && 'word2' && 'word3'}) { ...do stuff ; }
I want the if to be true if all the words are found, in any order. But it's returning true
if just word3 is found. I think it's ignoring word1 and word2.
I'm too new at Perl to know if it's even appropriate syntax, although it doesn't give any error currently.
I've also found a technique using $_ eq, but it doesn't seem to be matching the way I want:
Keep in mind, I never found any solution to match several values; so this && tactic is a semi-educated guess.if ( grep { $_ eq 'word1' && 'word2' } @allwords ) { ...do stuff; }
p.s. I don't know why the code format is so tiny in the preview.. I can hardly read the code. But hopefully
it's not that way when I post it.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Check array for several matches
by 1nickt (Canon) on May 10, 2017 at 22:25 UTC | |
|
Re: Check array for several matches
by tobyink (Canon) on May 10, 2017 at 22:41 UTC | |
|
Re: Check array for several matches
by kcott (Archbishop) on May 11, 2017 at 06:48 UTC | |
|
Re: Check array for several matches
by Marshall (Canon) on May 11, 2017 at 01:55 UTC | |
by Lotus1 (Vicar) on May 12, 2017 at 15:28 UTC | |
by Marshall (Canon) on May 13, 2017 at 00:00 UTC | |
by Lotus1 (Vicar) on May 13, 2017 at 00:24 UTC | |
|
Re: Check array for several matches
by ankitpati (Sexton) on May 11, 2017 at 13:38 UTC | |
by haukex (Archbishop) on May 11, 2017 at 13:54 UTC | |
by AnomalousMonk (Archbishop) on May 11, 2017 at 15:30 UTC | |
|
Re: Check array for several matches
by Lotus1 (Vicar) on May 12, 2017 at 15:03 UTC |