Range operators: two dots v.s. three dots
Author |
Message |
John Li #1 / 11
|
 Range operators: two dots v.s. three dots
Hi, I see the two dot operator '..' and three dot operator '...' in perlop. Without examples, I can't understand the difference. for(a..z) { print } for(a...z) { print } # the same! while(<>) { print if 1..5 } while(<>) { print if 1...5 } # the same! Would you help? Thanks.
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Jim Brita #2 / 11
|
 Range operators: two dots v.s. three dots
Quote: >Hi, > I see the two dot operator '..' and three dot operator '...' in perlop. >Without examples, I can't understand the difference. >for(a..z) { print } >for(a...z) { print } # the same! >while(<>) { print if 1..5 } >while(<>) { print if 1...5 } # the same! >Would you help? Thanks
The range operator is specially defined and otpimized for text operations. For the full info, and a clear explanation, type: perldoc perlop at the command line, and use /range to search forward.. There's several paragraphs. It is extremely useful for text block matching.
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Samuel Kilchenman #3 / 11
|
 Range operators: two dots v.s. three dots
Quote: > I see the two dot operator '..' and three dot operator '...' in > perlop. > Without examples, I can't understand the difference. > for(a..z) { print } > for(a...z) { print } # the same!
In list context there is no difference between the two operators Quote: > while(<>) { print if 1..5 } > while(<>) { print if 1...5 } # the same!
In scalar context the relevant part of perlop says: The range operator 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), use three dots ("...") instead of two. This is useful if you want to do things like "get the lines between lines starting with the same digit (character, substring, regexp or whatever)" as in this mini example: while(<DATA>) { chomp(); my $seq_numb = $_ =~ /^(1|2|3)/ ... /^$1/; if ($seq_numb ne "" and $seq_numb > 1 and $seq_numb !~ /E0$/) { print "Sequence Number: $seq_numb", " Matched String: $_", "\n"; } Quote: }
__DATA__ 123 aaa aab 132 bbb bbc bbd 213 ccc ccd cce 231 ddd 312 eee 321 This should print: Sequence Number: 2 Matched String: aaa Sequence Number: 3 Matched String: aab Sequence Number: 2 Matched String: ccc Sequence Number: 3 Matched String: ccd Sequence Number: 4 Matched String: cce Sequence Number: 2 Matched String: eee It doesn't print anything if the .. operator were used, because the "ranges" matched in this case are all the lines starting with the digits 1, 2 or 3.
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Abiga #4 / 11
|
 Range operators: two dots v.s. three dots
<URL::">
~~ Hi, ~~ I see the two dot operator '..' and three dot operator '...' in perlop. ~~ Without examples, I can't understand the difference. $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ perl -wne 'print if /foo/ .. /bar/' foobar barbar $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ perl -wne 'print if /foo/ ... /bar/' foobar $ Abigail -- perl -e '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %; BEGIN {% % = ($ _ = " " => print "Just Another Perl Hacker\n")}' -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==---------- http://www.newsfeeds.com The Largest Usenet Servers in the World! ------== Over 73,000 Newsgroups - Including Dedicated Binaries Servers ==-----
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Jonathan Stow #5 / 11
|
 Range operators: two dots v.s. three dots
Quote:
> Hi, > I see the two dot operator '..' and three dot operator '...' in perlop. > Without examples, I can't understand the difference. > for(a..z) { print } > for(a...z) { print } # the same!
When it is being used in a list context like this there is absolutely no difference in its behaviour. Quote: > while(<>) { print if 1..5 } > while(<>) { print if 1...5 } # the same!
In a scalar context (ie like this ) then the number of dots determines the way that the operands are evaluated : with two dots the two operands can be evaluated in the same iteration - that is two say that if both conditions fall on the same line - with three it will not evaluate the right hand operand until the next time. Of course you will not see this behaviour when you are implicitly using $. to match against as your example but you may see it if you are you (e.g.) using regex : [pigment.dircon.net] $ perl -e 'while(<>) { print if (/foo/ ... /bar/) }' foo hfgfg bar foo hfgfg bar uryjh uryjh [pigment.dircon.net] $ perl -e 'while(<>) { print if (/foo/ .. /bar/) }' foo hfgfh bar foo hfgfh bar dhjdh /J\ -- "Tony Blair. Make it so" - Patrick Stewart
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Samuel Kilchenman #6 / 11
|
 Range operators: two dots v.s. three dots
Quote:
in > <URL::">
> ~~ Hi, > ~~ I see the two dot operator '..' and three dot operator '...' in perlop. > ~~ Without examples, I can't understand the difference. > $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ > perl -wne 'print if /foo/ .. /bar/' > foobar > barbar > $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ > perl -wne 'print if /foo/ ... /bar/' > foobar > $
You must have a broken Perl...
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Jonathan Stow #7 / 11
|
 Range operators: two dots v.s. three dots
Quote:
> in >> <URL::">
>> ~~ Hi, >> ~~ I see the two dot operator '..' and three dot operator '...' > in perlop. >> ~~ Without examples, I can't understand the difference. >> $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ >> perl -wne 'print if /foo/ .. /bar/' >> foobar >> barbar >> $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ >> perl -wne 'print if /foo/ ... /bar/' >> foobar >> $ > You must have a broken Perl...
Er that is exactly the same behaviour that I demonstrated is mine broken as well ? /J\ -- "Is there no demand for mechanical pussies?" - Mrs Slocombe
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Samuel Kilchenman #8 / 11
|
 Range operators: two dots v.s. three dots
Quote:
>> in >>> <URL::">
>>> ~~ Hi, >>> ~~ I see the two dot operator '..' and three dot operator '...' >> in perlop. >>> ~~ Without examples, I can't understand the difference. >>> $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ >>> perl -wne 'print if /foo/ .. /bar/' >>> foobar >>> barbar >>> $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ >>> perl -wne 'print if /foo/ ... /bar/' >>> foobar >>> $ >> You must have a broken Perl... >Er that is exactly the same behaviour that I demonstrated >is mine broken as well ?
No your Perl isn't broken because your example demonstrated exactly the opposite behavior. According to the documentation the first of Abigail's examples must print: foobar the second example must print: foobar barbar
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Randal L. Schwar #9 / 11
|
 Range operators: two dots v.s. three dots
OK, so I came up with my own test, staring at this: 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), use three dots ("...") instead of two.) The right operand is not evaluated while the operator is in the "false" state, and the left operand is not evaluated while the operator is in the "true" state. [...] Here's my test: for (qw(none leftright none right none)) { printf "%20s .. %3s ... %3s\n", $_, (scalar (/left/../right/) ? "YES" : "NO"), (scalar (/left/.../right/) ? "YES" : "NO"); } And the results: none .. NO ... NO leftright .. YES ... YES none .. NO ... YES right .. NO ... YES none .. NO ... NO And that looks correct... the /right/ on the "..." didn't test on the same line as the /left/ did... it had to wait further. I'm not sure how Abigail got those results. $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ perl -wne 'print if /foo/ .. /bar/' foobar barbar $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ perl -wne 'print if /foo/ ... /bar/' foobar $ Because I get exactly the opposite when I run my 5.5.3 Perl against that code! Abigail, what Perl are you running? print "Just another Perl hacker," -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Abiga #10 / 11
|
 Range operators: two dots v.s. three dots
MCMXCIII in <URL::">
{} {} I'm not sure how Abigail got those results. {} {} $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ {} perl -wne 'print if /foo/ .. /bar/' {} foobar {} barbar {} $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ {} perl -wne 'print if /foo/ ... /bar/' {} foobar {} $ {} {} Because I get exactly the opposite when I run my 5.5.3 Perl against {} that code! Abigail, what Perl are you running? 5.005_03. I used cut-and-paste, then yank-and-copy, and removed the '.' on the wrong line. My post should have read: $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ perl -wne 'print if /foo/ ... /bar/' foobar barbar $ perl -wle 'for (qw /foobar barbar bazbar/) {print}' |\ perl -wne 'print if /foo/ .. /bar/' foobar $ Abigail -- sub f{sprintf'%c%s',$_[0],$_[1]}print f(74,f(117,f(115,f(116,f(32,f(97, f(110,f(111,f(116,f(104,f(0x65,f(114,f(32,f(80,f(101,f(114,f(0x6c,f(32, f(0x48,f(97,f(99,f(107,f(101,f(114,f(10,q ff))))))))))))))))))))))))) -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==---------- http://www.newsfeeds.com The Largest Usenet Servers in the World! ------== Over 73,000 Newsgroups - Including Dedicated Binaries Servers ==-----
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Michael Buda #11 / 11
|
 Range operators: two dots v.s. three dots
Quote:
> Hi, > I see the two dot operator '..' and three dot operator '...' in perlop. > Without examples, I can't understand the difference. > for(a..z) { print } > for(a...z) { print } # the same! > while(<>) { print if 1..5 } > while(<>) { print if 1...5 } # the same! > Would you help? Thanks.
'perldoc perlop' and scan for 'Range Operator' hth- --
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
|