panitaxx has asked for the wisdom of the Perl Monks concerning the following question:

Hi:

I want to know how can I access a Hash of Multidimensional array.
I created the hash from a database:

while ($aref = $sth->fetch) { push @{ $HoA{$l} } , [ $aref->[0] , $aref->[1] ]; }

How can I access with a foreach the first elements that are in the array of $HoA{$key}.

Replies are listed 'Best First'.
Re: Hash of multidimensional array
by buckaduck (Chaplain) on Mar 16, 2001 at 03:37 UTC
    I assume that you realize you are creating a hash with only one key/value pair in this code. Maybe that makes sense in a larger program. For your sake I hope so.

    If so, you can (for example) access the first bottom-level array reference via: $HoA{$key}[0] and the first element in that array via $HoA{$key}[0][0]

    buckaduck

Multidimensional array not a hash of, I think.
by frankus (Priest) on Mar 16, 2001 at 16:57 UTC

    Could you clarify:

    • You refer to the item as a hash of arrays when it appears to be a hash of array names.
    • What is the point of having a seperate array of arrays for each key? Why not have a hash of an array of arrays? $HoA{$key}-[$sth->fetch()]
    Any hoo..
    • To access the first element of an array with the name given by $HoA{$key} which refers to an array of arrays:${$HoA{$l}}[0]->[0];
    • The method to reference the hash of arrays is $HoA{$key}->[0]
    My chance to waggle my finger and be boring
    • In SQL: rows even in sequence only have an arbitrary value in a hash, unless you're using a field as the key.
    • $aref is an array reference to an array of arrays (rows x fields) since fetch is an alias for fetchrow_arrayref, so why dereferencing it? It's make sense if there are more rows in the SQL being returned than you need, however this is wasteful - rewrite the SQL.

    --
    
    Brother Frankus.
    Me: Curse you buckaduck for writing so tersely, I repeated your erm.. stuff and made me look dumb.
    Brain: You made yourself look dumb.
    Me: You mean I'm really good at something? Woo hoo!!