Perl | reverse() Function

reverse() function in Perl when used in a list context, changes the order of the elements in the List and returns the List in reverse order. While in a scalar context, returns a concatenated string of the values of the List, with each character of the string in the opposite order.

Syntax: reverse List

Returns:
String in Scalar Context and List in List Context.



Example 1: In List Context:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl -w
  
# Defining list of +ve Integers to the array
@array1 = (20, 30, 40, 50, 60, 70);
print reverse(@array1), "\n";
  
# Defining list of Integers to the array
@array2 = (1, -2, 3, 4, -5, 6);
print reverse(@array2), "\n";

chevron_right


Output:

706050403020
6-543-21

Example 2: In Scalar Context:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl -w
  
# Defining string to be reversed
$string = "Hello World";
print scalar reverse("$string"), "\n";
  
$string = "Geeks For Geeks";
print scalar reverse("$string"), "\n";

chevron_right


Output:

dlroW olleH
skeeG roF skeeG


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.