Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: modifying a string in place

by graff (Chancellor)
on Jan 20, 2006 at 04:12 UTC ( #524394=note: print w/replies, xml ) Need Help??

in reply to modifying a string in place

Thank you for demonstrating this use of the range operator:
for ( @whatever ) { if ( 1 .. 4 ) { # enter this block during the first four iterations, then neve +r again } }
It's documented in the perlop man page, but I had never seen it before, and when I first looked at your post, I thought "that can't be right -- how could that possibly work". But once I ran it myself, and studied the man page carefully, I saw the beauty of it, and I'm grateful for that.

Update: Having said that, it seems I'm still missing something. I stepped through the OP code with "perl -d", and sure enough, the "if ( 1 .. 4 )" worked as the OP says it should: the "if" block is entered on the first four iterations, then the "else" block is entered on the remaining iterations.

But when I tried the simplest possible snippet to do the same basic thing, it didn't work that way:

$_ = 0; while ($_<6) { $_++; if ( 1 .. 4 ) { print "$_: True\n"; next; } print "$_: False\n"; } __OUTPUT__ 1: False 2: False 3: False 4: False 5: False 6: False
When I study the man page again, this is sort of what I should have expected (but I think I should have gotten at least one "True" output):
In scalar context, ".." returns a boolean value. The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various editors. Each ".." operator maintains its own boolean state. It is false as long as its left operand is false. Once the left operand is true, the range operator stays true until the right operand is true, AFTER which the range operator becomes false again. It doesn't become false till the next time the range operator is evaluated. It can test the right operand and become false on the same evaluation it became true (as in awk), but it still returns true once.
I get even more puzzled when I try this variation, which should evaluate to false on the first iteration (but doesn't -- and it doesn't flip-flop either):
$_ = 0 while ($_<6) { $_++; if ( 0 .. 3 ) { print "$_: True\n"; next; } else { print "$_: False\n"; } } __OUTPUT__ 1: True 2: True 3: True 4: True 5: True 6: True
What am I doing wrong here?

Replies are listed 'Best First'.
Re^2: modifying a string in place
by BrowserUk (Pope) on Jan 20, 2006 at 06:20 UTC

    The constant form of the flip-flop only operates against $.. Ie. The line number of the current file being read. Hence the first loop below produces the output expected, but the second does not.

    #! perl -slw use strict; while( <DATA> ) { print if 1..4; } for ( 1 .. 8 ) { print if 1..4; } __DATA__ line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://524394]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2018-01-19 02:07 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (215 votes). Check out past polls.