How to reverse a String and List in Perl| Reverse function

This tutorial shows how to reverse a string and a list in Perl.

Perl reverse function does two things

  • In Scalar Context, it reverses the string character order.
  • In List Context, it reverses the order of elements in a list.

Perl Reverse Function example

The reverse function is employed to reverse the characters of a given string.

Syntax:

reverse(variable);

The reverse function is employed to reverse the characters of a given string.

Here is an example

my $str="Hello";
$reverseStr = reverse($str);

print "$reverseStr";

Output:

olleH

In the above example, the original string stored in $str has its characters reversed and stored in a new variable $reverseStr.

Printing an Array or List in Reverse Order in Perl

To reverse the order of elements in an array.

  • Declare an array with literal syntax, such as @numbers = (1,2,3,4,5);
  • Pass the array to the reverse function.
  • In list context, reverse changes the order of elements in the array to reverse it.
  • Return the reversed list.
  • Print the list in reverse order.
my @number=(1,2,3,4,5);
@reversenumbers = reverse(@number);

print "@reversenumbers";

Output:

5 4 3 2 1ssssssssssssssssssssssszz

How to Reverse string in Perl with code example

There are multiple methods to reverse a string:

  • Use the rveverse function To reverse string characters in reverse order, Please follow the steps.

    • Declare a string with the literal value
    • pass the string to the reverse function
    • reverse in scalar context reverse the string in reverse order, the character is changed to the last character, the last character to the first character
    • Return a reverse string
    • Print the string in reverse order
    my $name="welcome";
    $reverseName = reverse($name);
    
    print "$reverseName";
    

    Output:

    emoclew
    
  • an example to reverse a string entered by a user.

    print "Please Enter a text: ";
    my $str = <STDIN>; # Read input from the user
    
    my $newStr = reverse $str;
    
    print "$newStr\n";
    

In the above example, It prompts a user to enter a text and store the variable into a variable.

use the reverse function to return the reverse of characters in a string. Finally, print the variable.

  • reverse a string using split and join functions

    my $str = "hello";
    # String splint into a character array
    my @chars = split //, $str;
    # reverse Character array, join into a string
    my $revers_str = join '', reverse @chars;
    
    print "Input String: $text\n";
    print "Output String: $revers_str\n";
    

Used array functions and here are steps.

The string is converted into an array of characters using split //. The array is reversed using the reverse function, and the elements are joined using join. Finally, the reversed string is printed to the console.

Reverse each Word in a Perl string with an example

Reversing Each Word in a Perl String with an Example

my $text = "Text contains a multiple words";
# Split text in a word array
my @words_array = split(/\s+/, $text);

# iterate each word of an array
foreach my $word (@words_array) {
    # Reverse a word
    my $reverse_str = reverse $word;
    print "$reverse_str ";
}

In this Example:

  • The string is split into words using split(/\s+/, string), which returns an array of words.
  • Each word is iterated through using a foreach loop.
  • Each word is reversed using the reverse function, then printed to the console.

In Summary, These examples show multiple ways to reverse a string or list in Perl.