# 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<Paul Michael< you have to type qw<Paul Michael>.
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 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
* Note A common misconception is that a list in scalar context will also return its size. This is untrue. In fact, there is no such thing as a list in scalar context: using the comma operator in a scalar context does not create a list, instead it evaluates each of its arguments, left to right, and returns the last one:
* $name = ('Paul','Michael','Jessica','Megan');
* print "The last name in my list is $name\n";

### 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 print function is being given a list of 6 arguments: the string 'My names are: ', each of the four values in @names, and the string ".\n". Each argument is printed separated by the value of the $, variable (that defaults to the empty string), resulting in the values from the array being 'squished' together:
In the second example, the print 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 $" variable (that defaults to a single space):
* Note Both the $, and $" variables can be changed to any string you like. For example, to separate the array's items with a comma and a space instead of just a space:
* print "My names are: @names.\n";
* My names are: Paul, Michael, Jessica, Megan.
* You generally do not want to do that as this may cause problems in other parts of your program depending on the default values of those variables though! A safer way to print your arrays with custom separator will be explained later.

### 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 $ 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? $#array will return it for you:
A common mistake is to do this:
In fact @array[0] is a slice (that is, a sub-array of an array) that contains one element, whereas $array[0] is a scalar that contains the value 1.

### Common array functions
## Command line arguments

As you may wonder, Perl scripts support command line arguments. The entire list of parameters is stored in the array @ARGV, with the first entry containing the first command line argument. If no command line parameters were passed, @ARGV 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