Perl | Reverse an array Last Updated : 23 Sep, 2018 Comments Improve Suggest changes Like Article 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 Comment More infoAdvertise with us Next Article Perl | Reverse an array G Gautam Karakoti Follow Improve Article Tags : Misc Perl Reverse Perl-String Practice Tags : MiscReverse Similar Reads Reverse an Array in Java Reversing an Array is a common task in every programming language. In Java, there are multiple ways to reverse an array. We can reverse it manually or by using built-in Java methods. In this article, we will discuss different methods to reverse an array with examples.Let us first see the most common 4 min read Reverse an Array in JavaScript Here are the different methods to reverse an array in JavaScript1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array.JavaScriptlet a = [1, 2, 3 3 min read Perl | Array Slices In Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable. Arrays can store any type of data and that data can be acce 3 min read Perl | reverse() Function 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 Li 1 min read Reverse an array using Stack Given an array arr[] of size n, the task is to reverse the array using Stack.Examples:Input: arr = [10, 20, 30, 40, 50] Output: 50 40 30 20 10 Explanation: Upon reversing the array becomes [50, 40, 30, 20, 10]. Therefore, the output is 50 40 30 20 10.Input: arr = [ 1 ]Output: 1Explanation: Reversing 4 min read JavaScript Array reverse() Method The JavaScript Array reverse() method reverses the order of the elements in an array in place. The first array element becomes the last, and the last element becomes the first, and so on.It modifies the original array and returns a reference to the reversed array. We can also use the Array.toReverse 3 min read Array Reverse - Complete Tutorial Given an array arr[], the task is to reverse the array. Reversing an array means rearranging the elements such that the first element becomes the last, the second element becomes second last and so on.Examples: Input: arr[] = {1, 4, 3, 2, 6, 5} Output: {5, 6, 2, 3, 4, 1}Explanation: The first elemen 15+ min read Perl | Arrays In Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable. Example: @number = (50, 70, 46); @names = ("Geeks", "For", 6 min read Reverse an array upto a given position Given an array arr[] and a position in array, k. Write a function name reverse (a[], k) such that it reverses subarray arr[0..k-1]. Extra space used should be O(1) and time complexity should be O(k). Example: Input: arr[] = {1, 2, 3, 4, 5, 6} k = 4 Output: arr[] = {4, 3, 2, 1, 5, 6} We strongly reco 6 min read JavaScript Array toReversed() Method The toReversed() method in Javascript arrays returns a new array with the elements in reversed order and the original array remains unaffected. The toReversed() method, introduced in ECMAScript 2023, offers a way to reverse the order of elements in an array. Unlike the sort() method, it creates a ne 1 min read Like