Perl Topics





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Perl Tutorial
Introduction Introduction
Installing Perl Installing Perl
Basics of Perl Basics of Perl
Scalar Variables Scalar Variables
Operators Operators
Control Structures Control Structures
Lists Lists
Array Array
Arrays Manipulation Arrays Manipulation
Arrays Functons Array Functions
Hash Hash
Hash Functions Hash Functions
String Functions String Functions
Regular Expression Regular Expression
Regular Expression Functions Regular Expression Functions
Numerical Functions Numerical Functions
List Functions List Functions
User Defined Function User Defined Function
File Handling File Handling
Forums Ask Your Doubts
Feedback Feedback
 





reverse Function in Perl


Tutorials Perl

Topic

What is reverse Function in Perl?



Explanation

The reverse() is used to flip a string or list.

Syntax:
     reverse(list)
     scalar reverse(string)

In the above syntax reverse function takes a "string" as well as a "list" as argument.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    @arr = (1,2,3,4,5,6);
    print "Reversed List Structure", reverse(@arr), "\n";
    $str = "Hiox India";
    print "Reversed String Value ", scalar reverse("$str"), "\n";
Result:
     Reversed list structure:654321
     Reversed String Value:aidnI xoiH 

In the above example first an array elements are reversed, then the string "Hiox India" is reversed.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    %name = ('Tom', 26, 'Peter', 51, 'Jones', 23);
    %inverted = reverse %name;
    while (($key, $value) = each(%name))
     {
       print $key.", ".$value."<br/>";
     }
Result:
    Jones, 23
    Peter, 51
    Tom, 26

In the above reverse Function example, a hash "%name" is reversed and stored in another hash "%inverted", then the values are printed using a loop.






A Note
Simple introduction, basic CGI perl programming codes with examples. Do send your feedback or suggestions on this tutorial. This is a copyright content.

Other Links

web hosting