That's not exactly what the difference is. In list context .. and ... work exactly the same way as you can see by running code like this:

my @a1 = 1 .. 10; my @a2 = 1 ... 10; print "@a1\na2\n";

The difference between them comes when you use the operator in a scalar context. In this case the operator returns false until the left operand is true. It then continues to return true until the right operand is true, at which point it returns false again (this is why it is sometimes known as the flip-flop operator). The difference between the two versions is that with two dots the operator evaluates its left operand and if it is true the operator evaluates the right operand immediately. It is therefore possible for the operator to switch from false to true and back to false on one evaluation. With three dots, if the left operand is true, the operator doesn't evaluate the right operand until the next evaluation.

As an example, imagine you have a file and you are interested in lines starting with the one containing 'BEGIN' and ending with the one containing 'END'. You can write code like this:

while (<FILE>) { if (/BEGIN/ .. /END/) { # do stuff } }

The difference is illustrated by the behaviour if you have a line that contains both 'BEGIN' and 'END'. The code as shown above will evaluate true (from 'BEGIN') and then will immediately evaluate false (from 'END') and the nett result is that it will return false. If you used the three dot version of the operator, it would evaluate true and evaluation would stop there. The right operand would not be evaluated until the next line is processed.

Both of these behaviours can be useful, therefore both operators exist :)

--
<http://www.dave.org.uk>

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


In reply to Re: Range (aka flip-flop) operator Re: Re: Re: Defining Arrays by davorg
in thread Defining Arrays by skullbowl

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.