Perl Lists

We will explore below the Perl lists and some ways to use and manipulate them. After scalar, the list is the second most important type of data in Perl language. If you remember, by scalar we mean one thing or object, like a number, a string or a reference.

There are situations when you want to refer to some collections of such things, which you can represent in Perl in three possible ways: literal lists, arrays and hashes.

The literal list (or list) is just a group of scalars used to initialize an array or a hash. The elements of a list can be numbers, strings or any other types of scalar data.

Each element of the Perl lists can be accessed by a numerical index, the first element of the list having the index 0, the next one 1, and so on.

The elements of a list are enclosed in a pair of round parenthesis and are generally separated by commas.

Here are some list examples:

$v = "Hello World!";
(1, 17, 'two numbers', $v, 12.27) 
(1, 17, 'two numbers', '$v', 12.27)

The first list has 5 elements and contains the numbers 1 and 17, the string 'two numbers', the string 'Hello World!' and the number 12.27. The second list will have as the fourth element the string '$v' composed by two characters: '$' and 'v'.

A convenient way to define the Perl lists is qw (quote word) operator which help you avoid writing too many quotation marks, but be aware of the case where you have embedded whitespaces (spaces, tabs or newlines) in some list elements. In this last case, we need to use the quotation mark and comma characters to delimitate the list elements:

qw(blue red yellow green)
("dark blue", "blue", "red", "yellow", "green")

The first list has 4 elements that are the strings "blue", "red", "yellow" and "green". In the second list we couldn’t use the qw operator because its first element is the string "dark blue", composed by two words separated by space.

The Perl language is very flexible as we can have a list included in another list as in the next example:

print ('a', 'b', qw(c d e f), 'g', 'h');
# it will display "abcdefgh"

We can access the list elements by using the index (which is called sometimes subscript) as you see in the example below:

print qw(blue red yellow green white)[1];

where the printing result is the "red" string.

Another operator used in the Perl lists is the range operator defined by the symbol ".." which you can use to create a list from a range of letters or numbers. This operator expands all the intermediate values between the left and right values. You can see how it works in the next example:

print (12..17, "-", abc..abi),"\n";

This line of cod will display "1214151617-abcabdabeabfabgabhabi". The first list element will be composed by all the numbers between the first value 12 and the last value 17, i.e. "1214151617".

If we don’t have any element in a list, we say that this is an empty list: () which we can use to initialize an array with a zero length.

You may use literal scalars to initialize scalar variables, analogues as you may use literal lists to initialize arrays or hashes. You need array and hash variables when you want to store the literal Perl lists in memory and to access and manipulate them.

Don’t make confusion between a list and an array or hash. There is the same difference between lists and arrays as between scalars and numbers. Scalars are variables that may contain numbers, but they are not numbers. Similarly, an array is a variable that can be initialized with a list.

Before we end up with Perl lists, we mention the module List::Util (search for it at CPAN if you are interested in) which contains a selection of useful subroutines to manipulate lists. It includes: getting the largest/smallest number or string, adding the elements of a list and returning the sum, use conditionals to return an element that matches some criteria, and much more.

Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order

return from Perl Lists to Perl Basics

Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!