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

Re: This code is just freaky...

by arturo (Vicar)
on Jul 27, 2001 at 21:47 UTC ( [id://100390]=note: print w/replies, xml ) Need Help??


in reply to This code is just freaky...

It's a funky snippet that processes a certain block of lines in the file. Specifically, this code ignores lines between ones that contain "abcd" and ones that contain "def".

The next tells you the code goes back to the block with the label LINE (so nothing in the loop below this line gets executed), and it does so while the following condition is true :

from the point that $line matches "abcd" to the point where it matches "def"

The range operator .. is the key here; notice that it occurs in scalar context, not list context. It's false until the regex on the left matches, where it flips to true, and remains true until the regex on the right matches.

Read it as "everything in between" and you'll have a good idea of what's going on here (that works for how it operates in list context, too)

This behavior is documented in perlop, under the heading "Range operators".

HTH!

perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'

Replies are listed 'Best First'.
Re: Re: This code is just freaky...
by dragonchild (Archbishop) on Jul 27, 2001 at 22:04 UTC
    I figured it out! The range operator stuff freaked me out a little, but the big trick is that

    foreach my $a (1 .. 5) { next while ($a != 2); }
    won't go to the while it's next to, but the outer foreach loop. Thus, you don't enter an infinite loop.

    A curious thing was that I wrote the quick program:

    my @strings = ('a', 'b', 'c', 'd', 'e', 'f'); while (my $s = shift @strings) { print "Looking at $s\n"; next while ($s =~ /a/ .. $s =~ /c/); print "$s is OK!\n"; }
    How does the range-op-boolean know that $s was a in a previous check. It seems to be maintaining state between invocations.

    Update:

    my @strings = ('a', 'b', 'c', 'd', 'e', 'f'); while (my $s = shift @strings) { print "Looking at $s\n"; my $value = $s =~ /a/ .. $s =~ /c/; print "The value is $value\n"; next if $value; print "$s is OK!\n"; }
    This gives a very interesting output.

    Looking at a The value is 1 Looking at b The value is 2 Looking at c The value is 3E0 Looking at d The value is d is OK! Looking at e The value is e is OK! Looking at f The value is f is OK!
    I just find that '3E0' thing really odd...
      I love the flip-flop operator. The description in older Camel books is classic. Its befuddling until you get through the whole thing, and great to impress other (non-Perl) programmers with.

      The E0 suffix thing is a bit clunky. Perl 6 should do that better, using properties.

      —John

      '3E0' is a variation on "0 but true". You can discover that it's the last line by matching /E0$/
      (efficiency:  index $value,'E0' >= 0), or treat it as a number, which yields 3.

        p

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found