in reply to accessing variable vaule outside for loop

How about something like this:

#!/usr/bin/perl use strict; my @number; my @rev; @number = (1,2,3,4); my $index1 = @number; my $revIndex = 0; my $x = 0; for ($x= 0; $x < $index1 ; $x+= 1) { $revIndex = $index1 - $x; $rev[$revIndex] = $number[$x]; #case1# print "@rev \n" ; } #case2# print "@rev \n";

Michael

Replies are listed 'Best First'.
Re^2: accessing variable vaule outside for loop
by gaurav (Sexton) on Jul 06, 2013 at 12:48 UTC

    Hi Michael,but its output is some type of pyramid means,when I ran your code I got

    1 2 1 3 2 1 4 3 2 1 4 3 2 1

    But I need reverse of it,anyway thanks for it

      Just remove the print statement inside the for loop, and you will no longer get this "pyramid". I think this print statement was put there just to show you how the array get progressively populated.

      Thanks Michael,Its working fine

        glad to help.