in reply to explain use of increment operator in grep

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.

  • Comment on Re: explain use of increment operator in grep