why ($index) needs to be written in bracket for the grep to return an appropriate index number?

my $index returns a scalar and thus creates a scalar context.
In scalar context, grep returns the number of matching items (which is not what you want).

my ($index) returns a list and thus creates a list context.
In list context, grep returns the matching items (where your code assigns the first one).

why can grep's 2nd parameter be either an array or sequence of array index?

You are mistaken in thinking the 2nd parameter is an array or sequence. You are passing a whole list of arguments.

Since
0 .. $#coins
returns
0, 1, 2
then
my ($index) = grep($coins[$_] eq "fourthy", 0 .. $#coins);
is the same as
my ($index) = grep($coins[$_] eq "fourthy", 0, 1, 2);

Since
@coins
returns
$coins[0], $coins[1], $coins[2]
then
my @index2 = grep($_ ne "fourthy", @coins);
is the same as
my @index2 = grep($_ ne "fourthy", $coins[0], $coins[1], $coins[2]);

This has nothing to do with grep. The expressions @coins and 0..$#coins are evaluated before grep is even called.

>perl -le"$,=','; print @ARGV" twenty thirty forty twenty,thirthy,fourthy >perl -le"$,=','; print 0..$#ARGV" twenty thirty forty 0,1,2

By the way, you misspelled "thirty" and "forty".

both grep's $_ symbol seems to work differently..is this dependent on the 2nd parameter of grep? one works as a key, another as a value!

The first time the first argument is executed, $_ is aliased to the second argument.
The second time the first argument is executed, $_ is aliased to the third argument.
The third the first argument is executed, $_ is aliased to the fourth argument.
And so on.

There's no such things as keys and values, just the list of arguments you passed to grep.


In reply to Re: grep usage confusion by ikegami
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.