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

I could use some help figuring out this data structure. I believe it is a hash with a value of a two-deminsional array. Thanks.
$val{$file}[3][$num{$alias}{save}]

Replies are listed 'Best First'.
Re: Help explaining data structure
by pg (Canon) on Oct 03, 2005 at 03:09 UTC

    You were actually right and the data structure is a hash of two dimentional arrays, although a more Perlish expression might be array of array refs. By the way, the index of the second dimention is getting from a hash of hash refs.

    You can easily visualize this with the help of Data::Dumper:

    use Data::Dumper; use strict; use warnings; my $file = "afile"; my $alias = "somealias"; my %num; my %val; $num{$alias}{save} = 2; $val{$file}[3][$num{$alias}{save}] = 1; print Dumper(\%val);
    $VAR1 = { 'afile' => [ undef, undef, undef, [ undef, undef, 1 ] ] };
Re: Help explaining data structure
by neosamuri (Friar) on Oct 03, 2005 at 02:47 UTC
    %val is a hash of array of arrays.

    %num is a hash of hashes.

    I believe that was what you were asking for.

    UPDATE: Changed the $ to % as pointed out by ikegami

      True, but that should be %hash and %num.

      Another important bit of information is how each array/hash is used. They are typically used to store a list of elements or a set of fields (record/object). I'm guessing:
      %val is a hash (named list) of array (record/object) of arrays (ordered list).
      %num is a hash (named list) of hashes (record/object).

      Yes, I see now. Probably should have stared at it a while longer before posting. I wonder if there is a friendlier way to write it? I don't think the arrow notation can be applied.
        I don't think the arrow notation can be applied.

        Sure it can... These are all equivalent:

        1. $val{$file}[3][$num{$alias}{save}] 2. $val{$file}[3][$num{$alias}->{save}] 3. $val{$file}[3]->[$num{$alias}{save}] 4. $val{$file}[3]->[$num{$alias}->{save}] 5. $val{$file}->[3][$num{$alias}{save}] 6. $val{$file}->[3][$num{$alias}->{save}] 7. $val{$file}->[3]->[$num{$alias}{save}] 8. $val{$file}->[3]->[$num{$alias}->{save}]
        Of those, I would prefer number 6 with some whitespace added for readability.
        $val{$file}->[3][ $num{$alias}->{save} ]
        -sauoq
        "My two cents aren't worth a dime.";
        
        "Yes, I see now."

        You did see from the beginning ;-) array of array refs is the same as two dimentional array. Put in this way, two dimentional array is the logical view across languages, and array of array refs is the Perl way of realize the concept.

        Due to the fact that the second level arrays are not required to have the same length in Perl, they have to be refs, so that the information about their lengthes is not lost because of the flattening.

Re: Help explaining data structure
by sauoq (Abbot) on Oct 03, 2005 at 02:59 UTC

    You are basically correct, but that's not exactly a complete data structure. Rather, it's one element in a complex data structure.

    Working backwards, that element is in an array with the index given by $num{$alias}{save}. A reference to that array is the fourth element (index of 3) of another array. A reference to that array is held in a hash called %val and keyed by the stringified value of $file.

    If the structure of %val is homogenous, then you could say it is a hash of two-dimensional arrays. We can't tell that by looking at one element though.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Help explaining data structure
by planetscape (Chancellor) on Oct 03, 2005 at 08:32 UTC

    Have a look at Re: How can I visualize my complex data structure?. I still run data structures through one or another of these techniques when I am not sure what's going on. :-)

    (I particularly like the last two visually-oriented techniques because that's just the way my brain works...)

    HTH,

    planetscape