Perl | Reverse an array Last Updated : 23 Sep, 2018 Comments Improve Suggest changes 2 Likes Like Report Reverse an array or string in Perl. Iterative Way: Iterate over the array from 0 to mid of array. Swap the arr[i] element with arr[size-i] element. Perl #Perl code to reverse an array iteratively #declaring an array of integers @arr = (2, 3, 4, 5, 6, 7); # Store length on array in $n variable $n = $#arr; #Print the original array print "The original array is : "; for $i (0 .. $#arr) { print $arr[$i], " "; } #run a loop from 0 to mid of array for my $i (0 .. $#arr/2) { #swap the current element with size-current element $tmp = $arr[$i]; $arr[$i] = $arr[$n-$i]; $arr[$n-$i] = $tmp; } #Print the reversed array print "\nThe reversed array is : "; for $i (0 .. $#arr) { print $arr[$i], " "; } Output: The original array is : 2 3 4 5 6 7 The reversed array is : 7 6 5 4 3 2 Using Inbuilt Function: Perl has an inbuilt function to reverse an array or a string or a number. Perl #Perl code to reverse an array using inbuilt function reverse #declaring an array of integers @arr = (2, 3, 4, 5, 6, 7); #Print the original array print "The original array is : "; for $i (0 .. $#arr) { print $arr[$i], " "; } #store the reversed array in @rev_arr @rev_arr = reverse(@arr); #Print the reversed array print "\nThe reversed array is : "; for $i (0 .. $#rev_arr) { print $rev_arr[$i], " "; } Output: The original array is : 2 3 4 5 6 7 The reversed array is : 7 6 5 4 3 2 Create Quiz Comment G Gautam Karakoti Follow 2 Improve G Gautam Karakoti Follow 2 Improve Article Tags : Misc Perl Reverse Perl-String Explore BasicsPerl Programming Language2 min readIntroduction to Perl7 min readPerl Installation and Environment Setup in Windows, Linux, and MacOS3 min readPerl | Basic Syntax of a Perl Program10 min readHello World Program in Perl3 min readFundamentalsPerl | Data Types3 min readPerl | Boolean Values3 min readPerl | Operators | Set - 112 min readPerl | Operators | Set - 27 min readPerl | Variables4 min readPerl | Modules3 min readPackages in Perl4 min readControl FlowPerl | Decision Making (if, if-else, Nestedâif, if-elsif ladder, unless, unless-else, unless-elsif)6 min readPerl | Loops (for, foreach, while, do...while, until, Nested loops)7 min readPerl | given-when Statement4 min readPerl | goto statement3 min readArrays & ListsPerl | Arrays6 min readPerl | Array Slices3 min readPerl | Arrays (push, pop, shift, unshift)3 min readPerl List and its Types4 min readHashPerl Hash4 min readPerl | Hash Operations8 min readPerl | Multidimensional Hashes6 min readScalarsPerl | Scalars2 min readPerl | Comparing Scalars6 min readPerl | scalar keyword2 min readStringsPerl | Quoted, Interpolated and Escaped Strings4 min readPerl | String Operators4 min readPerl | String functions (length, lc, uc, index, rindex)4 min readOOP ConceptsObject Oriented Programming (OOPs) in Perl7 min readPerl | Classes in OOP6 min readPerl | Objects in OOPs6 min readPerl | Methods in OOPs5 min readPerl | Constructors and Destructors4 min readPerl | Method Overriding in OOPs6 min readPerl | Inheritance in OOPs7 min readPerl | Polymorphism in OOPs4 min readPerl | Encapsulation in OOPs6 min readRegular ExpressionsPerl | Regular Expressions2 min readPerl | Operators in Regular Expression4 min readPerl | Regex Character Classes3 min readPerl | Quantifiers in Regular Expression4 min readFile HandlingPerl | File Handling Introduction7 min readPerl | Opening and Reading a File4 min readPerl | Writing to a File3 min readPerl | Useful File-handling functions2 min read Like