1. Home
  2. Computing & Technology
  3. Perl

Perl array reverse() function - Quick Tutorial

How to use the array reverse() function

From About.com

@REVERSED_LIST = reverse(@ARRAY);
Perl's reverse() function is used to reverse the order of an array. It should be noted that the function returns a reversed array but does not actually reverse the array passed into the function.
@myNames = ('Jacob', 'Michael', 'Ethan', 'Andrew');
@reversedNames = reverse(@myNames);
Think of the @myNames array as a row of numbered boxes, going from left to right, numbered starting with a zero. The reverse() function would copy the contents of the boxes from the @myNames array into the @reversedNames array, while reversing their order. The value of @reversedNames then becomes ('Andrew', 'Ethan', 'Michael', 'Jacob'), and @myNames remains the same.
More Perl Quick Tips

Explore Perl

More from About.com

  1. Home
  2. Computing & Technology
  3. Perl
  4. Programming Perl
  5. Perl reverse function reference - learn how to use Perl's reverse() function in this quick tutorial.

©2008 About.com, a part of The New York Times Company.

All rights reserved.