grep will only return those items from @list that satisfy the given condition. In this case, the grep condition is doing two things: it is incrementing an element in the %temp hash keyed by the element from @list, and it is testing to see if the value of the hash element is less than 2.

The increment is '++$temp{$_}', which means that the hash value is incremented before the "less than 2" test is performed (as opposed to '$temp{$_}++', which would increment after the test is done).

So, the first time a given string is encounted in @list, the resulting hash value satisfies the condition (incremented, it is less than 2). Each time the given string is encountered again, the increment puts the hash value above 1, so that the "less than 2" condition will fail, and grep will exclude that list item from the set that it returns.


In reply to Re: explain use of increment operator in grep by graff
in thread explain use of increment operator in grep by pglenski

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.