Perl grep does what Perl grep does. Perl grep is not like command line grep. Think of grep{} like a "filter" that operates on a list. If the thing within grep{$something} is TRUE, then the input is passed to the output, ie, a "filter"...THAT'S IT!

Ok, look at this:

my $t = 1; print grep {$t} qw( a b c d e ); What do you think that means? this is the same as: grep{1}qw( a b c d e ); and yields the exact same output as input list (a b c d e) grep {0} qw( a b c d e ); yields nothing! because this means foreach(a b c d e), pass each item to the output (to the left) if the thing within grep{} is true. BUT {0} is NOT true! So nothing is passed to your print. my $True_false = "False"; foreach (qw ( a b c d e )) { print if ($True_false eq "True"); print "" && $_; #another way #either way prints nothing!!! } Your very cute use of XOR is well, cute. my $t = 1; print grep {$t^=1} qw( a b c d e );
The above code "toggles" the value within the grep{} between TRUE and FALSE. So you get every other list element. Odd or even depending upon whether $t starts at 0 or starts at 1. This "how do I get the even things in the list" is cute, but maybe a bit too cute?

Anyway the code works exactly as expected.

Update: Maybe the problem is that you don't know what $t^=1 means?
$t+=1; means $t = $t + 1; #better as $t++, but no matter here..
$t^=1; means $t = $t ^ 1;


In reply to Re: grep function usage by Marshall
in thread grep function usage by rgadireddy

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.