in reply to Check array for several matches

I tried to write some straightforward code for you. Pay attention to the algorithm (the method of determining the result). Nothing really "fancy" is required here, just logical thinking about how to determine a successful result. "map" is just a kind of a foreach loop. Don't worry about that until you understand the simple loops below.
#!/usr/bin/perl use strict; use warnings; my @requiredMatches = ('word1', 'word2', 'word3'); my @testArray = qw (word2 abc word3 xyz word2 asdf word1 word2); my %requiredMatches; # set up a hash table to count occurences of # each word that is required to be there. # Start with "zero", haven't seen it yet. foreach my $requiredWord (@requiredMatches) { $requiredMatches{$requiredWord}=0; } # Now for each word in the input @testArray, increment it's # "seen" count, if and only if it is one of the words # that is being "tracked" meaning that a hash entry exists in # %requiredMatches for that word, i.e., $requiredMatches{$word} # # If you don't check for "exists", Perl will happily create a new # hash entry that is incremented. We don't want that here. # We only want the %requiredMatches hash to contain only the # "must have" words. Update: Well the "if" is not absolutely # required below because we are only going to count "zeroes", # but I prefer this formulation that doesn't generate unnessary # hash entries. foreach my $word (@testArray) { $requiredMatches{$word}++ if exists $requiredMatches{$word}; } # If every hash table entry got incremented (no zero initial # values left), then every required Word was seen at least once. my $countWordMissing=0; foreach my $requiredWord (keys %requiredMatches) { $countWordMissing++ if ($requiredMatches{$requiredWord} == 0); } print "testArray contains all words\n" if $countWordMissing==0;

Replies are listed 'Best First'.
Re^2: Check array for several matches
by Lotus1 (Vicar) on May 12, 2017 at 15:28 UTC

    My approach was almost the same as yours except I used a function so I could short circuit and return as soon an item was determined to be missing.

    use warnings; use strict; use Data::Dumper; my @allwords = qw( mary had a little lamb its fleece was white as snow + ); my @checkwords = qw( little lamb fleece everywhere ); print "All words were ", check_array_for_words(\@allwords, \@checkword +s) ? "" : "not ", "found.\n"; sub check_array_for_words { my ($ar_allwords, $ar_checkwords) = @_; my %hash; foreach (@$ar_allwords){ $hash{$_} = undef; } print Dumper(\%hash); foreach (@$ar_checkwords) { if (not exists $hash{$_}) { return 0; } } return 1; } __END__ $VAR1 = { 'had' => undef, 'lamb' => undef, 'a' => undef, 'fleece' => undef, 'little' => undef, 'white' => undef, 'as' => undef, 'was' => undef, 'mary' => undef, 'snow' => undef, 'its' => undef }; All words were not found.
      Yes, this will certainly work! I made an implicit assumption that there were very few check words (perhaps just 3) to be checked against perhaps a potentially very big list of other words. So I restricted my hash to be only the 3 checked words instead of all_words. But I think your code is just fine.

      I don't think it is necessary to beat this thing to death in all its variations. I don't know whether I actually succeeded or not, but I was attempting to address the OP's comments about searching the internet for hours and copying code that he didn't understand and that didn't work for him.

      I avoided map{} and grep{} (which are special kinds of foreach loops) and attempted to write very simple loops in the hope that the OP will take the time to understand them. I added a whole lot of verbiage in an attempt to explain an example of the algorithmic thought process. I am hoping that the OP learned not only a solution to this current problem, but also stuff that is helpful to future problems.

        The short circuit thing doesn't add much for three items, that's true. Probably isn't a noticeable difference for anything less than 3,000 really. I had already written mine up before I noticed yours so it seemed fitting to put it with yours.