Perl Programming/Array variables

Perl syntax includes both lists and arrays.

Lists
A list in perl is an ordered set of scalar values. It is represented in your code as a comma-separated sequence of values, which may or may not be contained in scalar variables. Lists can be used to make multiple assignments at once, and can be passed as arguments to several built-in and user-defined functions:

Alternate List Construction
When creating a list of several strings that do not include spaces, Perl provides a shortcut to get around typing multiple quotes and commas. Instead of you can use the qw// operator. This operator uses any non-alpha-numeric character as a delimiter (typically the / character), and encloses a space-separated sequence of barewords. A delimiter separates the command with the arguments. The above line is identical to the following:

and both are equal to this:

The last example uses the open and close parenthesis as a different delimiter. If there is an open and close version of the delimiter you choose, you need to use them both. Otherwise just repeat the same symbol twice. For example, you cannot type qw you have to type qw.

You can also abuse the glob syntax, when the strings do not include shell metacharacters:

List assignments
As shown above, lists can be used to make several assignments at once. If the number of variables on the left is the same as the number of values on the right, all variables are assigned to their corresponding values, as expected.

If there are fewer variables on the left than values on the right, the 'extra' values are simply ignored:

If there are more variables on the left than values on the right, the 'extra' variables are assigned the default undef value:

The existence of list assignment creates the ability to 'swap' two variables' values without the need of an intermediary temporary variable:

Arrays
An array in Perl is a variable that contains a list. An array can be modified, have elements added and removed, emptied, or reassigned to an entirely different list. Just as all scalar variables start with the $ character, all array variables start with the @ character.

Array Assignment
Arrays are assigned lists of values. The list of values can be arbitrarily large or small (it can even contain 0 elements).

That last example exemplifies a feature of Perl known as 'array flattening'. When an array is used in a list, it is the array's elements that populate the list, not the array itself. As stated above, a list is a set of scalar values only. Therefore, the @all</tt> array contains 1000 elements, not 2.

Arrays in scalar context
When an array is used in scalar context - either by assigning a scalar variable to the array's value, or using it in an operation or function that expects a scalar - the array returns its size. That is, it returns the number of elements it currently contains

Printing an Array
There are two general ways of printing the values of an array. You can either print the list of items in the array directly, or you can interpolate the array in a double-quoted string.

In the first example, the <tt>print</tt> function is being given a list of 6 arguments: the string 'My names are: ', each of the four values in <tt>@names</tt>, and the string ".\n". Each argument is printed separated by the value of the <tt>$,</tt> variable (that defaults to the empty string), resulting in the values from the array being 'squished' together: My names are: PaulMichaelJessicaMegan.

In the second example, the <tt>print</tt> function is being given exactly one argument: a string that contains an interpolated array. When Perl interpolates an array, the result is a string consisting of all values in the array separated by the value of the <tt>$"</tt> variable (that defaults to a single space): My names are: Paul Michael Jessica Megan.

Accessing Elements of an Array
The elements of an array are accessed using a numerical reference within square brackets. Because each item within an array is a scalar value, you need to use <tt>$</tt> when referencing a value. The first element of an array is number 0 and all the others count up from there.

A negative number will count down from the right side of the array. This means that -1 references the last element of the array and -3 references the third to last element. Let's see some examples:

What if you need to know the last index? <tt>$#array</tt> will return it for you:

A common mistake is to do this:

In fact <tt>@array[0]</tt> is a slice (that is, a sub-array of an array) that contains one element, whereas <tt>$array[0]</tt> is a scalar that contains the value 1.

Command line arguments
As you may wonder, Perl scripts support command line arguments. The entire list of parameters is stored in the array <tt>@ARGV</tt>, with the first entry containing the first command line argument. If no command line parameters were passed, <tt>@ARGV</tt> is an empty array.

The array functions and operators listed above can easily be used to detect the passed command line arguments and to detect the number of arguments provided.

Related articles

 * Data Structures/Arrays
 * List Functions
 * Array Functions
 * Perl Arrays

Perl/Tablice