On 6/5/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: snip
how do i declare loop and print a 4 dim array
snip
Arrays in Perl are all one dimensional, but multiple dimensions can be faked with references. Given an "multidimensional" array @a, you would access the first level with $a[0]. The value at this level would be an arrayref. You could then dereference and index into that array like this $a[0]->[0]. This is clunky so Perl lets you remove the arrow operator between the [] operators like this $a[0][0]. The value at this level is also an arrayref, so we can say $a[0][0][0] and so on until you get to a level that is not an array ref (in a four dimensional array it would be $a[0][0][0][0]). Now, since each level is either an array or an arrayref we can iterate over each level like this #!/usr/bin/perl use strict; use warnings; my @a = ( [ [ [ "0,0,0,0", "0,0,0,1" ], [ "0,0,1,0", "0,0,1,1" ], [ "0,0,2,0", "0,0,2,1" ], [ "0,0,3,0", "0,0,3,1" ], [ "0,0,4,0", "0,0,4,1" ], ], [ [ "0,1,0,0", "0,1,0,1" ], [ "0,1,1,0", "0,1,1,1" ], ], ], [ [ [ "1,0,0,0", "1,0,0,1" ], ], [ [ "1,1,0,0", "1,1,0,1" ], ] ] ); for my $aref (@a) { for my $aref (@$aref) { for my $aref (@$aref) { for my $item (@$aref) { print "$item\n"; } } } } #or for my $i (0 .. $#a) { for my $j (0 .. $#{$a[$i]}) { for my $k (0 .. $#{$a[$i][$j]}) { for my $l (0 .. $#{$a[$i][$j][$k]}) { print "position [$i][$j][$k][$l] is $a[$i][$j][$k][$l]\n"; } } } } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/