Perl array examples

Perl array examples FAQ: Can you share some Perl array examples (Perl array programming examples)?

After doing a lot of work with Perl arrays recently, I thought I'd post a little collection of Perl array examples out here today. The Perl array syntax isn't too hard, but there are a few common mistakes you can make, so hopefully a simple reference page will help.

My Perl array print function

Before getting into the Perl array examples, here's the code for a Perl function (subroutine) that I will use in all of the later examples to print the arrays I create:

sub print_arr() {
  my (@arr) = @_;
  print "\n";
  foreach $elem (@arr) {
    print "$elem\n";
  }
}

This is a simple Perl function, and just prints each array element that is passed in to it. (If you haven't seen an array passed into a Perl function before, this can also be a good example of working with an array in a Perl function.)

Simple Perl integer array examples

To begin my Perl array examples, here is some source code that shows several examples of Perl integer (int) arrays:

# (1) create a perl integer array the hard way
$nums[0] = 1;
$nums[1] = 2;
$nums[2] = 3;
&print_arr(@nums);

# (2) create a perl array an easier way
@nums = (1,2,3);
&print_arr(@nums);

# (3) create a perl int array using the range operator
@nums = (1..10);
&print_arr(@nums);

# (4) create a perl array using two integer ranges
@nums = (1..5,10..15);
&print_arr(@nums);

# (5) create an int array using the range operator with no parentheses
@nums = 1..10;
&print_arr(@nums);

As you can see, there are quite a few ways to create an integer array in Perl (and there are probably more ways than I have shown here).

One of the most important things to remember about arrays in Perl is that the name of an array begins with the '@' character. Whenever I've been away from Perl for a while I forget that.

As another important note, don't forget that Perl doesn't really care that these are all ints. You can define a mixed array like this, and Perl will still be very content:

@nums = (1, 2, 3, 'foo', 'bar');

In most programs you will likely create an array of integers, array of strings, or an array of something else, but generally speaking, Perl doesn't care what you put in your arrays.

Perl array examples - Creating Perl string arrays

Here are several examples of how to create a string array in Perl:

# (1) create a perl string array the hard way
$pizzas[0] = 'cheese';
$pizzas[1] = 'pepperoni';
$pizzas[2] = 'veggie';
&print_arr(@pizzas);

# (2) create a string array an easier way
@pizzas = ('cheese', 'pepperoni', 'veggie');
&print_arr(@pizzas);

# (3) create a string array an even easier way
@pizzas = qw(cheese pepperoni veggie);
&print_arr(@pizzas);

As you can see from that last example, when you create a string array in Perl, you can use a special syntax with the Perl qw operator. The 'qw' operator seems to mean 'quoted words' or 'quoted by whitespace' (depending on who you talk to). The important thing to know about the Perl qw operator is that it handles the quoting for you, which makes creating Perl string arrays very simple.

Another important thing to know about qw is that you can use characters other than the parenthesis character as a delimiter. For instance, here is the same example as the last line, using some of the most common delimiters I've seen in Perl programs from other authors:

@pizzas = qw/cheese pepperoni veggie/;

@pizzas = qw! cheese pepperoni veggie!;

@pizzas = qw# cheese pepperoni veggie#;

As a practical matter I normally see the parenthesis and forward-slash characters used with the Perl qw operator, but it's important to know that other characters can be used.

Perl array examples - Accessing Perl array elements

When accessing Perl array elements, there are at least two common ways to access the elements. First, the way I normally access them is inside a Perl foreach loop, like this:

foreach $pizza (@pizzas) {
  print "$pizza\n";
}

I've mentioned here before that I really like this syntax because it's easy to remember and easy to read.

The second common way to access Perl array elements is by using the index of each element. For instance, if you just want to print the first three elements in a Perl array, you can print them like this, using their indices:

print "$pizzas[0]\n";
print "$pizzas[1]\n";
print "$pizzas[2]\n";

The important thing to know here is that like the C programming language (and many other languages since C), Perl array indices begin with the number zero.

Perl array examples - Summary

I hope this collection of Perl array examples has been helpful. I've tried to include the most common Perl array programming techniques I use. For more Perl array programming examples, please see the Related block on this page, use our website search form, or leave a note in the Comments section below about other Perl array examples you'd like to see.