- Perl ›
- builtin functions ›
- here
reverse function - reverses the order of arrays or strings
Using the reverse function , array or string You can reverse the order of>. The reverse function reverses the order of an array when used on an array, but reverses the string when used on a string.
my $reversed = reverse $str; my @reversed = reverse @array;
Reverse function example
This is an example that reverses the order of strings using the reverse function.
use strict; use warnings; print "Invert string\n"; my $str = "abcdefg"; my $reverse = reverse $str; print "$reverse\n";
This is an example that uses the reverse function to reverse the order of the array.
use strict; use warnings; print "Invert array\n"; my @nums = (3, 6, 9); my @nums_reversed = reverse @nums; print "@nums_reversed\n";