In a list context, this operator is easy to use and understand:
#!/usr/bin/perl
foreach(1 .. 10) {
print;
}
foreach(A .. B) {
print;
}
It is much more confusing in a scalar context, and is often badly explained in books and webpages. It is very easy to misunderstand how it works at all, never mind the difference between using ".." vs. "...", which can trip you up badly. Let's look at the simplest cases first and then move to the more difficult:
#!/usr/bin/perl
while (<>) {
print "$. $_" if 3 .. 10;
}
That prints lines 3 to 10 of any input or file given to it. If we change it to "print "$. $_" if 3 ... 10;" it works the same way. But we aren't limited to line numbers. Let's create a file "g":
# stuff # stuff # end on same line as start START foo END # # end by itself END # more stuff # more stuff # another start START # and more stuff END # trailing stuff
And a Perl script to read it:
#!/usr/bin/perl
while (<>) {
print if (/START/ .. /END/);
}
Running that against our file produces:
START foo END START # and more stuff END
But with the three dot version:
#!/usr/bin/perl
while (<>) {
print if (/START/ ... /END/);
}
the results are different:
START foo END # # end by itself END START # and more stuff END
The difference is how the operator treats its left side and right side when they appear on the same line or out of order. Here's what's going on: with the two dot ".." version, the operator returns false until the left hand side is true. It then switches to returning true and keeps doing that until its right hand expression evaluates to true. But the operator REMAINS true until the next evaluation even so, so it only switches back to false when the next line is read. With the three dot "..." version, the right hand expression will not be evaluated while the left hand is false, and vice versa. Confusing? You bet.. be very careful when using these operators and test your logic against sample input very carefully. Personally, I'll usually write out a much longer loop maintaining my own flip-flops than use this, just because I often get tricked by its nuances. If your brain works better than mine, you might find these handy in quite a few situations.
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar