Perl Variables  «Prev  Next»
Lesson 6Perl Lists.
ObjectiveExamine the use of lists in Perl.

Lists in Perl

In Perl, an array is simply a list of scalars. In fact, Perl internally represents an array and a list in the same manner. In order to understand arrays, let us first take a look at what Perl calls a list. In Perl, the simplest form of a list is just a series of scalars separated by commas.
Parenthesis are almost always necessary around the list because the comma is of very low precedence. Here is an example of a list:

('a', 'b', 'c', 1, 2, 3, 4)

Alternately, if the list is made up of a sequential series of letters or numbers, the list can be constructed with the range operator (
..
):
('a' .. 'g', 1 .. 15)

Any of the items in the list can be scalar values, or scalar variables:
('a' .. 'g', $x, $y, $z, 1 .. 15)

The easiest way to understand an array is to know that it is just a list with a name assigned to it.

How is a Perl List different from an array?

In Perl, a list and an array are related but distinct concepts. They are often used interchangeably, but it's important to understand the differences:
  1. List: A list is a sequence of scalar values, enclosed in parentheses and separated by commas. Lists are immutable, which means you cannot change their elements directly. Lists are used in various situations, such as when assigning values to arrays or passing arguments to functions and subroutines. Lists do not have a variable name or a sigil[1] (like $, @, or %).
    Example of a list:
    (1, 2, 3, "a", "b", "c")
    
  2. Array: An array is a mutable, ordered collection of scalar values, indexed by a range of non-negative integers. Arrays are denoted by the @ sigil, followed by a variable name. You can access individual elements of an array using the $ sigil and square brackets with the index of the element. Arrays can be resized, and their elements can be modified.
    Example of an array:
    my @array = (1, 2, 3, "a", "b", "c");
    


Notice how the list is used to assign values to the array. Here's another example illustrating the difference between lists and arrays:
# List
my ($x, $y, $z) = (1, 2, 3);
print "$x, $y, $z\n";  # Output: 1, 2, 3

# Array
my @array = (4, 5, 6);
print "$array[0], $array[1], $array[2]\n";  # Output: 4, 5, 6

In this example, a list is used to assign values to scalar variables $x, $y, and $z. In the second part, a list is used to assign values to an array called @array. The array elements can be accessed using the $ sigil and square brackets with the index.
In summary, a list in Perl is an ordered sequence of scalar values, while an array is a mutable, ordered collection of scalar values. Lists are used to initialize arrays, as well as in other contexts like assigning values to multiple scalars or passing arguments to functions and subroutines.

Perl List Example

A list is an ordered collection of scalars. An array is a variable that contains a list. Programmers tend to use the terms interchangeably, but there is a big difference. The list is the data and the array is the variable that stores the data. You can have a list value that is not in an array, but every array variable holds a list (although that list may be empty). Figure 2-6 represents a list, whether it is stored in an array or not. Since lists and arrays share many of the same operations, just like scalar values and variables do, we will treat them in parallel.


Figure 2-6:. A list with five elements
Figure 2-6:. A list with five elements

In the next lesson,an array will be examined in detail.
[1] sigil : In computer programming, a sigil is a symbol affixed to a variable name, showing the variable's datatype or scope, usually a prefix, as in $foo , where $ is the sigil. Sigil, from the Latin sigillum, meaning a "little sign", means a sign or image supposedly having magical power.