Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: regex in html + from ... to

by bjelli (Pilgrim)
on Apr 02, 2001 at 13:39 UTC ( #68965=note: print w/replies, xml ) Need Help??

in reply to regex in html

If you are processing big files you might want to avoid slurping the whole thing at once. Here the range operator <kbd>...</kbd> comes in handy. When used in a scalar context it returns a boolean and does just what you need here:

while (<DATA>) { if (/$start/.../$end/) { print; } }

I'll try to explain what happens in detail:

The magic is in the three dots: When the first line is processed, the three dots are in the "false" state. They take the expression on the left (<kbd>/$start/</kbd>) and evaluate it. If the expression returns false everything stays the same, the three dots return false. If the expression returns true, the three dots return true and go into the "true" state.

The next time we come to the three dots, the expression on the right is evaluated. If it returns false, everything says the same: the three dots continue to return true. If the expression returns true, the three dots go back into the true state.

But once you've grokked all that, you just think of the whole while + if + ... construct as "from /$start/ to /$end/"

Brigitte    'I never met a chocolate I didnt like'    Jellinek

Replies are listed 'Best First'.
Re: Re: regex in html + from ... to
by AidanLee (Chaplain) on May 04, 2001 at 17:18 UTC

    The Camel 2nd Ed. States that the range operator is two dots '..' not three.


      It can be both .. and .... They have subtly different effects.

      With two dots, it's possible for both the start and end checks to be true on the same line. This means that the operator goes from false to true and back to false again on one evaluation. With three dots, if the start check is true, then the end check isn't checked until the next evaluation - thus forcing at least one iteration with the operator returning true.


      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://68965]
[choroba]: ♪ ♫ ♬ Moon on the water... the fire in the sky... ♪ ♫ ♬

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (15)
As of 2017-07-25 09:30 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (368 votes). Check out past polls.