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

Hello.

I'd like to ask a question regarding arrays and using/changing their contents from a loop.

For example:

@array = qw/v w x y z/; for(1 .. $nn) { # I was expecting the array to shift # at every loop, and I could call on # the first position every time. print OUT $array[0] , "\n"; $slide = shift(@array); push(@array, $slide); }
Obviously the original array isn't changing.
Is this what one would refer to as a scope issue/problem?

Thank you.

-Baratski

Replies are listed 'Best First'.
Re: Pushing arrays around inside a loop ;-)
by davido (Cardinal) on Jan 06, 2005 at 08:07 UTC

    When I give $nn some value, and get rid of the 'OUT' filehandle, so that I can run the script you've posted, I find that it does what should be expected: If $nn is set to 11, for example, you get:

    v w x y z v w x y z v

    The array most certanly is being modified by shift and push, and the snippit you provided demonstrates that. What were you expecting? Maybe there is a scoping issue at work, but if there is, you haven't shown it to us.

    And more importantly, what is it that you're trying to do?


    Dave

      Thanks guys. Actually I am getting the output that you got. Sorry I should have been more clear.

      This is what I'm looking to get from the array:

      v w x y z
      w x y z v
      x y z v w

      And so on... If I'm shifting the contents of the array, shouldn't the value of the first position change, ie; $array[0]?

      What I'm doing is working through 2 arrays, one not shown that simply repeats itself by resetting a counter. The other is the one I am having trouble with.

      I'm trying to do this:

      Employee1 v
      Employee2 w
      Employee3 x
      Employee4 y
      Employee5 z

      Employee1 w
      Employee2 x
      Employee3 y
      Employee4 z
      Employee5 v

      Employee1 x
      Employee2 y
      Employee3 z
      Employee4 v
      Employee5 w

      And so on... It's a rotation schedule that I'm working on for my crew.

        Use modulo division and you dont have to modify the arrays at all.

        my @emps=map "employee_$_",1..5; my @codes='v'..'z'; my $n=3; for my $j (0..$n-1) { for my $i (0..$#emps) { print $emps[$i],":",$codes[($i+$j) % @codes],"\n"; } print "\n"; } __END__ employee_1:v employee_2:w employee_3:x employee_4:y employee_5:z employee_1:w employee_2:x employee_3:y employee_4:z employee_5:v employee_1:x employee_2:y employee_3:z employee_4:v employee_5:w
        ---
        demerphq

Re: Pushing arrays around inside a loop ;-)
by ColtsFoot (Chaplain) on Jan 06, 2005 at 08:04 UTC
    What results were you getting, what errors were output?
    The following code seems to work for me
    #!/usr/bin/perl -w use strict; my @array = qw/v w x y z/; for(1 .. 5) { # I was expecting the array to shift # at every loop, and I could call on # the first position every time. print $array[0] , "\n"; my $slide = shift(@array); push(@array, $slide); }
Re: Pushing arrays around inside a loop ;-)
by gube (Parson) on Jan 06, 2005 at 09:50 UTC

    Hi,

    Just try this,

    @array = qw/v w x y z/; for(0 .. $#array) { $slide = shift(@array); push(@array1, $slide); } print @array1;

    o/p: vwxyz

    Regards,

    Gubendran.L