Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^5: Foreach Loops

by Roy Johnson (Monsignor)
on Mar 16, 2005 at 13:57 UTC ( #439944=note: print w/ replies, xml ) Need Help??


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

I'm glad you got a kick out it, but I really have to discourage you from using it for indexing. Can you imagine explaining it?

"What the hell is this mess?"
"Oh, I'm tracking the index of the element of the array."
"That's how you get an array index in Perl?! That's ridiculous. We're switching everything to Visual Basic®!"
The most common use of a flip-flop is to extract a range of lines from a file. You set the left side to match the start of the range, and the right side to match the end of the range. If either operand is a constant expression, it is implicitly compared to $. (the line number). That is what kept me from using a simple expression like (1==1)..(1==0). Actually, now that I think about it, the expressions I used ought to be constant expressions.

Anyway, I did think of a couple interesting alternatives for always-true flip-flops.

$index = (!$index)...(!$index); #or $index = ($.||1)..($.&&0);
Note that in the former, I have to use the 3-dot version, so that both operands aren't evaluated on the same pass. $index starts out zero, so the left side is true. After that, it's non-zero, so the right side is false.

The second example just incorporates a variable into an expression whose truth is constant. I used $. because of its association with the operator, but any variable would do.


Caution: Contents may have been coded under pressure.


Comment on Re^5: Foreach Loops
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://439944]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2014-12-25 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls