Things You'll Need:
- Computer
-
Step 1
Obtain array data from the main program. Another array "new_array" will contain reversed data.
-
Step 2
Remove the last element from the initial array using the "pop" function.
-
Step 3
Add that element as the first one to the new array with "shift" function.
-
Step 4
Repeat Steps 2 and 3 until the initial array becomes empty; all its elements get transferred to the new one in reverse order.
-
Step 5
The working Perl program is below
#Program starts
my $array=[qw(1 2 6 7.4 25 9 12.6 2 6)]; #Example array
my $new_array=[];
print "@$array \n";
reverse_array($array, $new_array);
print "@$new_array \n"; #Printing the new array
exit;
sub reverse_array {
my($array, $new_array)=@_; #Step 1. Read initial array
while(@$array) {
my $entry=pop (@$array); #Step 2. remove the last element of the initial array
push @$new_array,$entry; #Step 3. Add that element to the new array as the first
}
} -
Step 6
The program output is as follows
1 2 6 7.4 25 9 12.6 2 6 <---initial array
6 2 12.6 9 25 7.4 6 2 1 <---new array
The array is reversed.














