"and" (the logical short-circuit operator) only evaluates the right hand side, if the left hand side is "true". That's because, if the left hand side is false, the entire statement can't possibly be true, so there's no point in evaluating the right hand side. That's part of what makes it a "logical" operator. It follows a path of logic. Test left hand side. If left hand side evaluates to true, test right hand side. If it evaluates to true also, the entire chain is true.
So on the first iteration, $counter is zero, which happens to be one value that Perl considers to be "false".
But you're incrementing $counter, with ++... why is it false? Because trailing ++ executes after the expression has been evaluated. Perl also allows for leading (versus trailing) ++ (autoincrement) operators. You could write the expression like this, and you'll find it works like a charm:
++$counter and print "found $users{$elem} if ( $users{$elem}{sales} - +$users{$elem}{payments} > 0 );
Dave
In reply to Re: idiom question
by davido
in thread idiom question
by geektron
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |