in reply to Mixing up indices in multidimensional hash

I agree it should probably be $tbls{'12p'} = ... not @{$tbls{'12p'}} (assuming you want an array reference).

Another thing to note is that using @$arrayref is generally preferred over $#array. I'll try to find some actual information to back that up :)

Update:

Look here for this:

The length of an array is a scalar value. You may find the length of array @days by evaluating $#days , as in csh. However, this isn't the length of the array; it's the subscript of the last element, which is a different value since there is ordinarily a 0th element. Assigning to $#days actually changes the length of the array. Shortening an array this way destroys intervening values. Lengthening an array that was previously shortened does not recover values that were in those elements. (It used to do so in Perl 4, but we had to break this to make sure destructors were called when expected.) If you evaluate an array in scalar context, it returns the length of the array. (Note that this is not true of lists, which return the last value, like the C comma operator, nor of built-in functions, which return whatever they feel like returning.)


I thought there was more to it, but I may be looking in the wrong place. Either way, the $#array is the last subscript while scalar(@array) is the actual length.
  • Comment on Re: Mixing up indices in multidimensional hash

Replies are listed 'Best First'.
Re^2: Mixing up indices in multidimensional hash
by punch_card_don (Curate) on Feb 16, 2005 at 20:06 UTC
    Yes, exactly. $#array is the last subscript while scalar(@array) is the actual length. Hence, using $#array as the last index in a for loop to loop through the array. I know some people prefer other styles, but I find that this keeps things very very explicit.

    Forget that fear of gravity,
    Get a little savagery in your life.