I think that grep is not the right tool for doing what you want. If I am right you want to iterate over all the items in array a and see if they are present in array b. Try the following:

my @SupportedPTs = ('Accounting','Dnd','Essay','FITB','Hotspot','Match +ing','MC','Narrative','SingleAnswer','SelfGradingGeneric','Sketch','S +tatic','TF'); my @ProblemTypes = ('Accounting','Dnd','test'); my %seen = (); foreach (@ProblemTypes) { $seen{$_} = 1; } for (@SupportedPTs){ if (! $seen{$_}){ print "$_ is supported\n"; } else { print "$_ is not supported\n"; } }

Outputs:

Accounting is not supported Dnd is not supported Essay is supported FITB is supported Hotspot is supported Matching is supported MC is supported Narrative is supported SingleAnswer is supported SelfGradingGeneric is supported Sketch is supported Static is supported TF is supported

Update:If you want to use grep (a solution less efficient than the previous), you don't need the temporary hash:

for my $sPT (@SupportedPTs){ if (! grep{/$sPT/} @ProblemTypes){ print "$sPT is supported\n"; } else { print "$sPT is not supported\n"; } }

Hope this helps,

citromatik


In reply to Re: Comparing arrays and returning false if an exception is found by citromatik
in thread Comparing arrays and returning false if an exception is found by reluctant_techie

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.