A Perl reverse array example

Perl reverse array FAQ: How do I reverse the contents of an array in Perl? (or, How do I use the Perl reverse function?)

It turns out that it's easy to reverse a Perl array, especially if you have already defined the sorting/comparing algorithm that Perl needs to sort the array.

A Perl reverse array example

For example, if you just want to reverse the lines in a Perl array that you've read from a text file, you can just use this Perl script:


# rcat   - a program to display the contents of a file in reverse order.
# author - alvin alexander, devdaily.com.

@lines = <>;
print reverse @lines;

As you can see from that example, the lines in the Perl array are printed in reverse with this statement:

print reverse @lines;

The important thing to note about the Perl reverse function is that it does not modify the array that is given to it as an argument., so you have to assign its output to another array. In the example above, the array @lines is not reversed; the Perl reverse array function simply returns the reversed contents of that array, which are then printed.

Perl reverse array - A second example

To demonstrate this -- and to also provide a second Perl reverse example -- assume you have a simple Perl array consisting of strings, like this one:

# a simple perl string array
@pizzas = qw(pepperoni cheese veggie sausage);

The wrong way to try to reverse this array is like this:

@pizzas = qw(pepperoni cheese veggie Works Anchovie sausage);

# this line of code is intentionally wrong:
reverse @pizzas;

print "@pizzas\n";

As mentioned, that line of code is wrong because the Perl reverse function doesn't reverse the array given to it, and you need to assign the output of the Perl reverse function to another Perl array variable, as shown here:

@pizzas = qw(pepperoni cheese veggie Works Anchovie sausage);

# this is correct, because I assign the reverse results back to the
# @pizzas array:
@pizzas = reverse @pizzas;

print "@pizzas\n";

This example prints the reversed array in the correct:

sausage Anchovie Works veggie cheese pepperoni

Perl reverse sorting of an array

Many times when you're working with Perl sorting, you need to supply a function or a block of code to the Perl sort function to tell it how to compare each array element during the sort process.

For instance, as I showed in an earlier Perl string array sorting tutorial, these lines of code will sort my array in case-insensitive, alphabetical order:

@pizzas = qw(pepperoni cheese veggie Works Anchovie sausage);
@sorted_pizzas = sort { "\L$a" cmp "\L$b" } @pizzas;
print "@sorted_pizzas\n";

You can read that Perl array sorting tutorial for more information on that code, but the important thing I want to show here is that you can easily reverse the way that sort function works. For instance, the code shown above prints my Perl string array in this order:

Anchovie cheese pepperoni sausage veggie Works

But the following code, which includes the Perl reverse function will reverse that sort order:

@pizzas = qw(pepperoni cheese veggie Works Anchovie sausage);

# reverse sort the perl array here:
@sorted_pizzas = reverse sort { "\L$a" cmp "\L$b" } @pizzas;

print "@sorted_pizzas\n";

When I print the @sorted_pizzas Perl array, the output now looks like this:

Works veggie sausage pepperoni cheese Anchovie

Perl reverse array and reverse sort - Summary

I hope these Perl reverse array and reverse sort examples have been helpful. Working with the Perl reverse function is surprisingly easy, well, at least after you've seen a few examples.