grep returns a list. In scalar context (my $index = ... for example) a list returns the count of elements in the list. Writing my ($index) = ... makes $index the first member of a one element list and grep's return list is in list context so the first element of grep's list is assigned to the first element of the left hand side list - $index.

$_ is always an alias to each element in the list for grep, map and in a for loop (whatever you use as the loop variable is the alias in the case of a for loop). In the first case you provide a list of numbers: 0 .. $#coins (indexes into the array). In the second case you provide a list of elements - the elements of @coins.

grep's "second parameter" is always a list. You provided two different lists in your sample code. The range operator generates a list.

Note that most often grep expressions are written using a block: grep {$_ ne "fourthy"} @coins.


Perl reduces RSI - it saves typing

In reply to Re: grep usage confusion by GrandFather
in thread grep usage confusion by adrive

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.