in reply to dynamic matrix
@matrix =([1,5,7], [2,3,4], [5,1,9], [7,8], [3]); for my $y(0..$#matrix) { my @row=@{$matrix[$y]}; for my $x(0..$#row) { print "Value $row[$x] at x $x y $y\n"; } }
Update: tphyahoo feels that the syntax would be more obvious if I used only array references here instead of arrays and I sorta agree (I think it runs the risk of encouraging cargo cult programming, because someone who doesn't understand references can think they know what's going on without understanding the underlying data structure), so here's a version with just references:
$matrix =[ [1,5,7], [2,3,4], [5,1,9], [7,8], [3] ]; for my $y(0..$#{$matrix}) { for my $x(0..$#{$matrix->[$y]}){ print "Value $matrix->[$y]->[$x] at y $y x $x\n"; } }
|
|---|