Broad Network

Range Operator in Perl

Perl Operators – Part 8

Perl Course

Foreword: In this part of the series, I talk about the Perl Range operator in list context. The range operator is a binary operator and it is ..

By: Chrysanthus Date Published: 12 Oct 2015


This is part 8 of my series, Perl Operators. In this part of the series, I talk about the Perl Range operator in list context. The range operator is a binary operator and it is ".." without the quotes. You should have read the previous parts of the series before reaching here, as this is a continuation. The range operator is useful for writing foreach (1..10) loops and for doing slice operations on arrays. It can also be used for string ranges.

Consider the following array:

    my @animals = ('dog', 'cat', 'cow', 'sheep', 'elephant', 'lion', 'tiger');

To return a copy of the values (list) in the array from index 1 to index 5, you can use:

    my $slice = @animals[1,2,3,4,5];

This slice is preferably written as in the following code using the range operator (try it):

use strict;

    my @animals = ('dog', 'cat', 'cow', 'sheep', 'elephant', 'lion', 'tiger');

    my @slice = @animals[1..5];
    print @slice;

foreach Loop
Remember that in Perl, “foreach” and “for” mean the same thing. The range operator can be used in the foreach loop as in the following code:

use strict;

foreach my $item (0..4)
            print $item * 2; print "\\n";

The output is:


The following code uses the range operator to print the position number and their corresponding values in an array.

use strict;

    my @arr = ('John', 'Peter', 'Mary');

     foreach (0..$#arr)
            print "Number ", $_+1, " is ", $arr[$_], "\\n";

The output is:

    Number 1 is John
    Number 2 is Peter
    Number 3 is Mary

Note that arrayName is found in the array definition statement and in the foreach list; that is why it is evaluated in the foreach loop. Remember that index counting for the array begins from zero and the last index for the array is $#arrayName.

Consider the following c-style for-loop:

use strict;

    my @arr = ('John', 'Peter', 'Mary');

     for (my $i=0; $i<@arr; ++$i)
            print "Number ", $i+1, " is ", $arr[$i], "\\n";

The following Perl-style for-loop operates faster than this one:

use strict;

    my @arr = ('John', 'Peter', 'Mary');

     foreach my $i (0..$#arr)
            print "Number ", $i+1, " is ", $arr[$i], "\\n";

The above three code samples do the same thing. The Perl-style samples, each operates faster than the c-style. So, you should always use the Perl-style foreach loop in Perl programs. The following is the recommended code for Perl to produce two-dimensional integers:

use strict;

    foreach my $i (0..2)
            foreach my $j (0..3)
                    print $i; print $j; print ", ";
            print "\\n";

The output is:

    00, 01, 02, 03
    10, 11, 12, 13
    20, 21, 22, 23

You can use the range operator to form a string range (and assign to an array). Read and try the following code that gives examples:

use strict;

    my @arr1 = "e".."n";
    print @arr1, "\\n";
    my @arrA = "E".."N";
    print @arrA, "\\n\\n";

    my @arr2 = "a".."z";
    print @arr2, "\\n";
    my @arrB = "A".."Z";
    print @arrB, "\\n\\n";

    my @arr3 = "3".."7";
    print @arr3, "\\n";
    my @arrB = "0".."9";
    print @arrB, "\\n\\n";

    my @mDays = ("01" .. "31");   #two digits days of the month
    print @mDays, "\\n\\n";

    my @hexDigits = (0 .. 9, "a" .. "f");   #two ranges for hexadecimal digits
    print @hexDigits, "\\n\\n";

The output is:






The range operator is non-associative.

That is it for this part of the series. We stop here and continue in the next part.


Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course



Become the Writer's Fan
Send the Writer a Message