gaurav has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have got struck since morning on-wards that how I can get the full reverse number outside the for loop

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

I mean to say that in case 1, I am getting full reverse value,but while trying to print same outside of the for-loop , I am getting last one (means 1 only).I want to get the full reverse value outside for-loop.How would I get that

Replies are listed 'Best First'.
Re: accessing variable vaule outside for loop
by eyepopslikeamosquito (Archbishop) on Jul 06, 2013 at 11:07 UTC

    If your intent is to reverse the @number array, why not write it like this?

    my @number = (1,2,3,4); my @rev = reverse @number; print "@rev\n";
    See reverse. If that is not your intent, please clarify.

      I want to do it without using any predefine function

        In Perl arrays, the index '-1' accesses the last element in the array. '-2' accesses the second-to-last. ...and so on, down to '- scalar @array', which will be the same element as '$array[0]'.

        This makes it easy to simultaneously count upward and downward; just do a numeric negation of sign, and adjust for off-by-one.

        @numbers = ( 100, 200, 300, 400, 500 ); $reversed[$_-1] = $numbers[-$_] for 1 .. @numbers; print "$_\n" for @reversed;

        Here's just about the only useful use of $[ (which should be avoided, even here).

        @numbers = ( 100, 200, 300, 400, 500 ); { no warnings 'deprecated'; local $[ = 1; # Don't do this... example only. $reversed[$_] = $numbers[-$_] for 1 .. @numbers; } print "$_\n" for @reversed;

        Dave

        I want to do it without using any predefine function

        I.e., "This is homework".

        try the push function to add an element to an array.

Re: accessing variable vaule outside for loop
by Laurent_R (Canon) on Jul 06, 2013 at 12:00 UTC

    This session under the debugger shows two possible ways of doing what you want.

    DB<1> @c = (1,2,3,4); DB<2> for ($i = $#c; $i>=0;$i--) {push @d, $c[$i]}; DB<3> x @d 0 4 1 3 2 2 3 1 DB<4> unshift @e, $_ for @c; DB<5> x @e 0 4 1 3 2 2 3 1

      Thank you @Laurent R ,you help me out.And thanks to others for their valuable suggestion

Re: accessing variable vaule outside for loop ( @foo = $bar )
by Anonymous Monk on Jul 06, 2013 at 10:11 UTC
Re: accessing variable vaule outside for loop
by mtmcc (Hermit) on Jul 06, 2013 at 12:31 UTC
    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

      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