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:
#!/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"; |
Output:
706050403020 6-543-21
Example 2: In Scalar Context:
#!/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"; |
Output:
dlroW olleH skeeG roF skeeG
Recommended Posts:
- Perl | Reverse an array
- Perl | each() Function
- Perl | uc() Function
- Perl | chr() Function
- Perl | exp Function
- Perl | hex Function
- Perl | log() Function
- Perl | oct() Function
- Perl | abs() function
- Perl | ord() Function
- Perl | sin() Function
- Perl | tell() Function
- Perl | int() function
- Perl | cos() Function
- Perl | prototype() Function
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.



