Challenge accepted :)

First, I went to read perlvar to refresh my memory on what the $" special variable is (list separator), so I was able to weasel the code down to this:

my @out = grep { -1 == index " @{find} ", " ${_} " } @a;

Already knowing that grep works 'backwards', I then followed that up with a peruse of perldoc -f index, and found out that the order of args are STRING, SUBSTR and POSITION. In this case, we're not using POSITION, as $_ is the value of the current array element, and is the SUBSTR to index().

So after I realized that "q@{find}q" was simply flattening the @find array into a string of its values separated by a different separator (which I changed back to the default whitespace), I was able to simplify a bit further:

my @out = grep { -1 == index " @find ", $_} @a;

Because although there are spaces around each element in the string-ified list, the whitespace regarding the SUBSTR is irrelevant, because index() is simply checking whether SUBSTR is in STRING, not checking for an exact match.

Because if this, I figured I could chop off two more chars:

my @out = grep { -1 == index "@find", $_} @a;

Lastly, in the previously mentioned perldoc -f index, it states that if the SUBSTR is not found in STRING, it'll return -1. In the last operation of the grep, you check to see if the return from index() is equal to -1, and if true (ie., the substr isn't found in the string-ified @find array), you push it onto the @out array, because grep does so when the block returns a true value. So you convert the expression to a true value after you've found that the original array doesn't match anything in the @find array.

Am I close? ;)

-stevieb


In reply to Re^2: Removing numerous entries from an array by stevieb
in thread Removing numerous entries from an array by stevieb

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.