• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Perl Range Operator

By Tony Lawrence | on Jun 16, 2005 | 0 Comment
CGI and Perl Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

Perl Range Operator

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.

Share this story:
  • tweet

Author Description

No Responses to “Perl Range Operator”

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 1,240Followers 494Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.