A shorter, more portable solution that uses only Perl built-ins.

Also, since you are starting out with Perl, knowledge of these techniques will be essential in your journey to Perl greatness!

Upto my %hash should be clear from the previous responses.

Enhancement (thank you, AnomalousMonk):

The deduplication code which I previously thought would be necessary is not actually required.

The next statement prints do stuff if the specified condition is met. Let us look at each part of the condition.

Evaluated in scalar context, arrays in Perl return the number of elements they contain. So @required returns the number of elements it contains.

@hash{@required} uses the hash-slice syntax to extract a list of values associated with the keys that happen to be members of @required. Trying to extract a value against a key which does not exist in a hash results in an undefined value.

Enhancement:

The defined test is not really required, as it is implicit.

grep { $_ } returns a list of all defined elements from the list previously returned. Recall that non-existent keys return an undefined value, which evaluates to false in the boolean context, so they are weeded out by this.

Enhancement (thank you, haukex):

In scalar context, grep returns the number of elements in the list that match the condition. Since that is what we need, we just use the value in scalar context directly.

I leave figuring out the remainder of the code as an exercise to the reader.

#!/usr/bin/env perl use strict; use warnings; my @allwords = qw( this is a list of words that is required for tests +); my @required = qw( this is a list of words that is required ); my %hash = map { $_ => 1 } @allwords; print "do stuff\n" if @required == grep { $_ } @hash{@required}; my @notthere = qw( this list is not there in allwords ); print "don't do stuff\n" unless @notthere == grep { $_ } @hash{@notthere};

I know this could get a bit overwhelming, so feel free to ask for any clarifications.


In reply to Re: Check array for several matches by ankitpati
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.