Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
We have all used the .. operator, but do we really know which powers it hold?

List context

Most often we use it in its most primitive form, in list context and ranging over a series of numbers.
for (1 .. 100) { # Do something with $_, which will start with the value 1, # increasing by one for every loop and stopping with the value 100. }

Magic string auto-increment

At other times we use it to make lists of string or letters
for ('a' .. 'z') { # $_ will start with the value 'a', then 'b' all the way up to 'z' } for ('aa' .. 'bb') { # $_ will start with the value 'aa', then 'ab', then 'ac' up to # 'az', then 'ba' and finally 'bb' }
A bit of carefulness, though since it can bite you hard, just think what would happen if we started with 'a' and then up to a user given string? Imagine if it were 'zzzzzzzzz', the generated list is very very long...

Scalar context

But the operator holds more power than this simple usage, for it can also work wonders in scalar context.
Here it is a bistable boolean operator. What it means is that it is a small state machine, which will return true or false depending on the current conditions.
while(<>) { print if 1 .. 5; #Print lines 1 trough 5 including. }

How it works

As any binary operator it takes two operands, the left and the right. It starts by evaluating to false until the left operand becomes true, thereafter it is true until the first iteration after the right operand becomes true, where the state reverts back to false. And then the cycle begins again.
Almost like the && operator, when in the false state, the right operand is not evaluated and, reversed, when in the true state, the left operand is not evaluated
One thing to notice is that when in the false state and the left operand evaluates to true, it will also evaluate the right side in the same operation, just like the && operator, but no matter if the right side is true or false it will return true. If you want to wait until next iteration with checking the right operand for the first time, you can use the .. operators sibling, the ... operator.
An additional feature that we saw already in the first example is that when the operands are constants, Perl will rewrite the code to $. == constant. So that it checks the current line number instead of just the constant.
Examples
Not seldom I find myself having to have a loop variable which keeps track of number of items I've seen within a range. In many cases however I could just extract it from the .. operator, which also tells me how many times it has currently matched.
while(<EMAIL>) { my $body = /^$/ .. eof; #First blank line in email is start of body my $header = 1 .. /^$/; print "looking at line ",int$body," in the body\n" if $body; print "looking at line ",int$header," in the header\n" if $header; }
Looking for all the paragraphs, with individually enumerated lines in a file? And we don't want the trailing blank line. For that we can use the fact that it returns a "nnnE0" string instead of just the number.
while(<>) { my $line = /\S/ ... /^$/; print "$line:$_" if $line && $line!~/E0$/; }

Conclusion

The .. operator has a lot of little known features and is generally underutilized. I hope you now have a better understanding of what powers it hold.

Now guess what these print
print for ('b'.. 'a')
print for ('a' .. 'B')
print for ('09' .. '08')
print for ('9' .. '8')

T I M T O W T D I

In reply to The .. operator explained by Cine

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (13)
    As of 2014-07-28 20:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (209 votes), past polls