in reply to Read array of arrays in reverse order

Have two indexes - on is initialized to largest index of your outer array, and the second one is initialized to the largest index of your inner array.

State to iteration, each time decrement both indexes by 1, until at least one of those indexes hits zero. With each iteration, take the element identifies by those two indexes - that's what you want.

Peter (Guo) Pei

  • Comment on Re: Read array of arrays in reverse order

Replies are listed 'Best First'.
Re^2: Read array of arrays in reverse order
by PeterPeiGuo (Hermit) on Apr 18, 2010 at 22:51 UTC

    You were probably looking for something more complicated, but your step 3 is wrong. Your (2,2) is not 1, it is 109 (this is 1-based index, just like you did, simply to avoid more confusion).

    Maybe what you are looking for is more like this:

    use warnings; use strict; my @my_arr=( [1, 11, 0, 0], [12, 109, 0, 0], [16, 1, 16, 0], [14, 14, 2, 0], [45, 16, 5, 4] ); my ($index1, $index2) = (5, 4); #used 1-based index for easy communica +tion while (1) { last if $index1 > 5 || $index1 < 1 || $index2 > 4 || $index2 < 1; print $my_arr[$index1 - 1][$index2 - 1], "\n"; $index1 = $my_arr[$index1 - 1][$index2 - 1]; $index2 --; }

    Peter (Guo) Pei