newbie question

H

hou

Hello, everyone
I am reading the "Beginning Perl" written by Simon Cozen.
In chapter 6, he gave an example as following.

------------------------------------------------
#!/usr/bin/perl
use warnings;
use strict;

open FILE, "gettysburg.txt" or die $!;
my @speech = <FILE>;

print "Last five lines:\n", @speech[-5 ... -1];
------------------------------------------------

I thought the [-5...-1] was a typo. It's supposed to be [-5..-1].
So, I tried to use "three dots". It turns out that three dots work.
Now, I am confused. Could anyone please tell what "three dots" means
in perl?

Thank you very much
Dean
 
P

Peter Wilson

hou said:
Hello, everyone
I am reading the "Beginning Perl" written by Simon Cozen.
In chapter 6, he gave an example as following.

------------------------------------------------
#!/usr/bin/perl
use warnings;
use strict;

open FILE, "gettysburg.txt" or die $!;
my @speech = <FILE>;

print "Last five lines:\n", @speech[-5 ... -1];
------------------------------------------------

I thought the [-5...-1] was a typo. It's supposed to be [-5..-1].
So, I tried to use "three dots". It turns out that three dots work.
Now, I am confused. Could anyone please tell what "three dots" means
in perl?

Thank you very much
Dean

Hi

look at perlop help (perldoc perlop) page from there the answer is written:

In scalar context, ``..'' returns a boolean value. The operator is bistable,
like a flip-flop, and emulates the line-range (comma) operator of sed, awk,
and various editors. Each ``..'' operator maintains its own boolean state.
It is false as long as its left operand is false. Once the left operand is
true, the range operator stays true until the right operand is true, AFTER
which the range operator becomes false again. It doesn't become false till
the next time the range operator is evaluated. It can test the right operand
and become false on the same evaluation it became true (as in awk), but it
still returns true once. If you don't want it to test the right operand till
the next evaluation, as in sed, just use three dots (``...'') instead of
two. In all other regards, ``...'' behaves just like ``..'' does.

Peter
 
T

Tad McClellan

hou said:
Would you please tell me how to locate the posting guidelines?


I'll do better than that. I tell you how you can locate lots of things.

There are "search engines" that can locate things on the web.

A popular search engine is www.google.com.

Entering

perl posting guidelines

in the little box there will find them.


Or, read the newsgroup regularly, they are posted twice each week.
 
B

Bart Lateur

A. Sinan Unur said:
Each ".." operator maintains its own boolean state. It is false as long
as its left operand is false. [...]

You're quoting from the decription of the bahaviour of the range
operator in scalar context. Clearly, the OP's code shows the use of
"..." in list context, which is what confuses him. Actually, it confuses
me, too. I think it must be considered as one of these cases:

In all other regards, "..." behaves just like ".." does.
 
T

Tad McClellan

Gunnar Hjalmarsson said:
Maybe it would be a good idea to append that URL to the bottom of
every message, like they do at ciwac.


I do not think that is a good idea.

It is already the top hit at Google and posted twice a week.

It should be the rare person that finds usenet/clpmisc before
finding Google, so not many people will benefit from a
reminder on every single post.

Plus, it is kind of "in your face", which I object to.

The "cost" is higher than the "benefit" in _my_ CBA,
it would be a lot of work for a questionable benefit...

Would that be possible, Tad?


.... making the answer to that question moot. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem Splitting Text String 2
Regex basic question 2
(..) and (...) question 4
Perl RegExp question 20
newbie question 3
Newbie Question 9
CSV confusion newbie question 1
Newbie question 7

Members online

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,262
Latest member
EffiePju4

Latest Threads

Top