Welcome to WebProNews Breaking eBusiness and Search News
Advertise | Newsletter | Sitemap | News Feeds News Feed 
 WebProNews Search Part of the iEntry network iEntry inc. 

Perl Range Operator (.. And ...)

A.P. Lawrence
Expert Author
Published: 2005-06-17

WebProNews RSS Feed


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.

*Originally published at APLawrence.com

Receive Our Daily Email of Breaking eBusiness News


About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com

WebProNews RSS Feed

More Articles

Contact WebProNews
Advertisement





TOP NEWS

Targeted Information for Business
WebProNews is part of the iEntry network

Internet Business: Marketing: Small Business:
WebProNews MarketingNewz SmallBusinessNewz
WebProWorld AdvertisingDay PromoteNews
EcommNewz SalesNewz EntrepreneurNewz

Software: Search Engines: Web Design:
WebMasterFree Jayde B2B DesignNewz
NetworkingFiles SearchZA FlashNewz
SecurityConfig SearchNewz WebSiteNotes

Developer: IT Management: Security:
DevWebPro ITManagement SecurityProNews
DevNewz SysAdminNews SecurityConfig
TheDevWeb NetworkingFiles NetworkNewz

The iEntry Network consists of over 100 web publications reaching millions of Internet Professionals. Contact us to advertise.
eBUSINESS RESOURCES






 Advertise | Contact Us | Corporate | Newsletter | Sitemap | Submit an Article | News Feeds
 WebProNews is an iEntry, Inc. ® publication - $line) { echo $line ; } ?> All Rights Reserved
About WebProNews
WebProNews is the number one source for eBusiness News. Over 5 million eBusiness professionals read WebProNews and other iEntry business and tech publications.

WebProNews provides real-time coverage of internet business.

Free Email Newsletters:
WebProNews SearchNewz
WebProWorld DevWebPro
Marketing SecurityNews
Plus over 100 other newsletters!

Send me relevant info on products and services.


WebProWorld
Ten most recent posts.

NetworkingFiles
Featured Software

WebProNews in the News
View all recent mentions of WebProNews from around the world!

Recent Articles On ...
Google eBusiness
Yahoo Ask Jeeves
MSN Blogs
Search Engines Blogging
Affiliate Programs Marketing
eCommerce Advertising
eBay Sun Microsystems
AOL Adsense
Microsoft Adwords
Oracle IBM
Amazon Apple
SEM Mac
SEO iPod
Adsense XBox
PR Adobe



iEntry.com WebProWorld RSS Feed WebProWorld Contact WebProNews Print Version Email a friend Bookmark us