To reverse an array in Perl, you can use the reverse function.
In this example,
@int_array
containing integers.reverse
function on @int_array
to reverse the elements.@int_array = (1, 2, 3, 4, 5);
@reversed_array = reverse @int_array;
print "Reversed Array: [@reversed_array]\n";
Reversed Array: [5 4 3 2 1]
In this example,
@str_array
containing strings.reverse
function on @str_array
to reverse the elements.@str_array = ("apple", "banana", "cherry");
@reversed_array = reverse @str_array;
print "Reversed Array: [@reversed_array]\n";
Reversed Array: [cherry banana apple]
In this tutorial, we learned How to Reverse an Array in Perl language with well detailed examples.