Skip to content
Related Articles

Related Articles

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:




#!/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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :