[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Multidimensional array question



|Sure they do, you just need to get the syntax right. Consider:
|$myArray[1][2][3] = 'Hello, earth';
|$#myArray == 1 && @myArray == 2
|$#{$myArray[1]} == 2 && @{$myArray[1]} == 3
|$#{$myArray[1][2]} == 3 && @{$myArray[1][2]} == 4

Yes, I'm replying to my own message. :-) After rereading it, I realize
that something I was trying to imply didn't come through at all and I
should have just stated it. That is, because perl multidimensional arrays
aren't the same as multidimensional arrays in other programming languages
(I would argue they're much better), the size of the second and subsequent 
subscripts *depends upon the value of the first subscript*.

Consider:

$m[0] = 'Greetings, earthling.';	# myArray's too hard to type :-)
$m[1][23] = 42;
$m[2] = {'a' => 4, 'b' => 3};
$m[3][5] = 23;

Then,

$#m == 3 && @m == 4
$#{$m[0]} isn't defined and will result in a runtime error
$#{$m[1]} == 23 && @{$m[1]} == 24
$#{$m[2]} isn't defined and will result in a runtime error
$#{$m[3]} == 5 && @{$m[3]} == 6

So the size of the second level arrays need not be the same, or even
defined, which means you need to be careful doing things like:

$m[$#m][$#{$m[$#m]}]

Brian

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch