@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