in reply to Pushing arrays around inside a loop ;-)

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

Replies are listed 'Best First'.
Re^2: Pushing arrays around inside a loop ;-)
by Baratski (Acolyte) on Jan 06, 2005 at 09:42 UTC
    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

        Hey, demerphq, that worked like a charm! Thank you so much. I made a couple of hashes to substitute your output with employee names and work areas. I'll study this modulo division that you used. I've never worked with it before.

        Thanks again to everyone that helped me out. You all are very helpful when I come here from time to time with a problem.

        -Baratski