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;

In reply to Re: Check array for several matches by Marshall
in thread Check array for several matches by Bman70

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.