perl function reverse
The reverse
function in Perl takes a list or a string as input and returns the elements in the reverse order.
Here's an example of using reverse
on a list of strings:
my @fruits = ('apple', 'banana', 'cherry', 'date'); my @reversed_fruits = reverse @fruits; print "@reversed_fruits\n"; # prints 'date cherry banana apple'Sourc:ewww.theitroad.com
In this example, the reverse
function is applied to an array @fruits
, which contains four strings. The function returns a new array with the elements in the reverse order, which is stored in @reversed_fruits
. The print
statement then outputs the reversed array.
Here's an example of using reverse
on a string:
my $string = "Hello, world!"; my $reversed_string = reverse $string; print "$reversed_string\n"; # prints '!dlrow ,olleH'
In this example, the reverse
function is applied to a string $string
, which contains the text "Hello, world!". The function returns a new string with the characters in the reverse order, which is stored in $reversed_string
. The print
statement then outputs the reversed string.