Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

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";



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";


dlroW olleH
skeeG roF skeeG
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!