Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re: Defining Arrays

by skullbowl (Monk)
on May 17, 2001 at 18:56 UTC ( [id://81259]=note: print w/replies, xml ) Need Help??


in reply to Re: Defining Arrays
in thread Defining Arrays

Thanks for the many solutions. I was thinking that Andye's post has a typo error when he used
(15...94)
the {..} operator has an additional {.}! But it works fine.

Learnt 2 things !

use Text::Soundex; print soundex ("Skullbowl");
RESULT : S414

Replies are listed 'Best First'.
Range (aka flip-flop) operator Re: Re: Re: Defining Arrays
by andye (Curate) on May 18, 2001 at 13:00 UTC
    skullbowl - the difference between '..' and '...' is that '...' includes the final value.

    So (1...3) will give you (1,2,3) whereas (1..3) will give you (1,2). Can be a crucial difference, as in Lethal Weapon.

    andy.

    update: Dave's right. Forget you've read this node - apart from the Lethal Weapon reference. You can remember that if you want, but forget the rest - it's wrong.

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-24 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found