Perl | reverse() Function
Last Updated :
07 May, 2019
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:
@array1 = (20, 30, 40, 50, 60, 70);
print reverse ( @array1 ), "\n" ;
@array2 = (1, -2, 3, 4, -5, 6);
print reverse ( @array2 ), "\n" ;
|
Output:
706050403020
6-543-21
Example 2: In Scalar Context:
$string = "Hello World" ;
print scalar reverse ( "$string" ), "\n" ;
$string = "Geeks For Geeks" ;
print scalar reverse ( "$string" ), "\n" ;
|
Output:
dlroW olleH
skeeG roF skeeG
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...