Perl Crash Course: Arrays and Lists

Share This Post

Arrays and Lists

Sometimes working with one piece of data just isn’t enough. You might want to specify lists of data, and save those lists in a variable. For this purpose, Perl gives us the list and array constructs.


Lists are just that – a comma-separated list of data specified between parentheses. You can add as many pieces of data (strings, numbers, variables) to a list as your computer’s memory can hold. Here’s an example of a list:

(1, 2, 'some string here', "hello $world", $myvar);   # plain list - not very useful though

As you can see in the example above, the elements in a list in Perl don’t have to contain the same kind of data like you’ll see in some other programming languages (only int’s, char’s, strings…). However, in the example above there isn’t much you can do with that list just the way it is. You’ll probably want to access its elements and assign it to variables:

($num1,$num2,$string1,$string2,$a_var) = (1, 2, 'some string here', "hello $world", $myvar);

All the values of the list on the right have been assigned to the variables in the list on the left. Now, suppose you don’t want the second element of the list on the right. You can use undef as a placeholder in the list on the left:

($num1, undef, undef, $string2,undef) = (1, 2, 'some string here', "hello $world", $myvar); # only the first and fourth values are kept

That last undef wasn’t really necessary. That’s because if you assign one list to another, if the list on the right has more values than the list on the left, the exceeding values will be automatically trashed. The undefs in the middle, however, are required so you can get the fourth position.

For cases where you want only one element of a list, you can select an element of a list by placing its index between [ ] right after the closing ). Indexes in Perl start in 0 by default and are numbered from left to right. You can change the startup index value by setting the variable $[, but you don’t want to do that unless you REALLY know what you’re doing. To access the list elements from right to left, use negative indexes, starting at -1 for the right-most element, then -2 for the second to last, and so on.

$some_var = (1, 2, 'some string here', "hello $world", $myvar)[0];    # $some_var now holds 1
$another_var = (1, 2, 'some string here', "hello $world", $myvar)[-1]; # $another_var gets a copy of $myvar
$and_yet_another = (1, 2, 'some string here', "hello $world", $myvar)[(2+2-1)*6-17]; # the crazy math evaluates to 1, so you get the second element of the list

To get more than one value of a list, you can use multiple indexes separated by commas:

($num1,$string2) = (1, 2, 'some string here', "hello $world", $myvar)[0,3]; # only the first and fourth values are assigned

More about that in the chapter about Slices.

Now, suppose you need to use the same list over and over, and just don’t feel like typing everything all the time. You need an Array.


An array is the kind of variable that Perl gives you to keep your lists in. All array names start with @ (stylish A for Array) and follow the same naming rules as scalars (except for the $ part).


To assign a list to an array, just do it like you would a scalar. You can also use the range (..) operator:

@colors = ('red', 'blue', 'yellow'); # assign @colors a list of 3 values
@ranges = (1..10,"a".."z","XX".."XZ"); # from 1 to 10, a to z, and XX to XZ

To access individual elements, use the same [ index ] construct:

print $colors[0];  # prints out 'red' to the screen

Whoah – hold on a sec!! You saw the $ instead of @ in the example above, right? The reason we did that is because we’re accessing one element of the array, and as we saw in the Scalars chapter, one of anything is a scalar. And this is where some people get confused. You see, $colors and $colors[0] are 2 totally different things (there’s yet a third element to this confusion, as you’ll see when we get to Hashes). The former is the scalar variable $colors, and the latter is the first element of the array @colors. To avoid mistakes while reading Perl code, be sure to check for any [ or { (not there yet) after the variable name.

Here are some array operations that you can use:

@copy_of_colors = @colors;  #  @copy_of_colors now contains 'red', 'blue', and 'yellow'
@reversed = reverse @copy_of_colors;  # yellow, 'blue', 'red'
$first_value = shift(@reversed); # $first_value gets 'yellow', and @reversed is left with 'blue', and 'red' only

The $# construct allows you to access the last index value of the array. E.g.: For our @colors array, we would get value 2 (indexes start with 0, right?) for $#colors (again, $#colors starts with a $ because it’s one value). This being said, another form of accessing the last element of any value is to do this:

$last_element = $colors[$#colors]; # same as $colors[2]

We will get deeper into scalar and array functions when we reach Gettin’ jiggy wit it.


One very important thing you MUST know and understand is that Perl treats arrays according to context. There is scalar context and list context. They can be implicit or explicit. This is how it works:

@colors = ('red', 'yellow', 'blue');   # set up the @colors array
$count = @colors;  # count now holds number 3, because @colors has 3 elements
$count = scalar(@colors);  # same thing, but called explicitly

When we assigned @colors to $count, since $count is a scalar, Perl understood that we’re talking in scalar context. When you call an array in scalar context, you’ll get the number of elements inside that array.

If you need to print out an entire array, there is also a small catch:

print @colors;  # prints out 'redyellowblue'
print "@colors";  # prints out 'red yellow blue'
print '@colors';  # prints out '@colors' - no interpolation with single quotes, remember?

« Scalars | TOC | Hashes »

More To Explore

Wordpress plugins in australia

bbPress Notify (No-Spam) Documentation

bbPress Notify (No-Spam) Documentation The bbPress Notify (No-Spam) is a free plugin that lets you control who gets notifications of new Topics and Replies in

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.