http://www.perlmonks.org?node_id=439799


in reply to Re^2: Foreach Loops
in thread Foreach Loops

I'm so glad you asked!

I am using a flip-flop as a counter. Of the conditions I gave it, the left side is always true and the right is always false, so the expression is always true. It is a property of the operator that it returns a count of the number of consecutive times it has been true. So subtracting one from that gives you the index.

Unless there are redos or nexts, which would throw it off. So I really should have written

my $index = 0; for my $line (@arr) { next if $index % 4 == 0; print "$index = $line\n"; } continue { $index = (1=~//..1=~/0/); }
Well, actually, I shouldn't have written it at all as a solution to this question. But I like putting little-used operators into the spotlight so that people might think of them when they actually are appropriate.

Caution: Contents may have been coded under pressure.