Perl data structure reference question

I almost feel abusive asking for this here on the SDMB since it’s totally not a general interest question. On the other hand there’s an expert on everything here and we’re all generally much nicer to Perl newbies… can someone give me a hand with what I think is a simple, basic, God-I-feel-stupid-for-having-to-ask problem with Perl?

If I have this hash:



%HoA = (
        flintstones        => [ "fred",   "barney"         ],
        jetsons            => [ "george", "jane",  "elroy" ],
        simpsons           => [ "homer",  "marge", "bart"  ],
      );

Then something like print scalar (keys %HoA) gives me the number of keys. Easy enough – %HoA refers to the hash as a hash. If I do this:


my $flints = $HoA{'flintsones'};

Then $flints contains a reference to the array “contained” in the value for key “flintstones”, right? That is, key “flintstones” can only be scalar, so it contains the address of the array. So I should be able to use the array pointed to by $flints as @flints, isn’t that correct? So I should be able to do something like print scalar @flints to get the array size. That doesn’t work, though.
By extension, I think I should be able to


print $flints[0];

…in order to print “fred” – also fails.

I’ve read all kinds of documentation about references in Perl, and it’s just flying way over my head, which is odd because I do perfectly fine in Object Pascal, C, and Objective-C with pointers and handles. Weird.

Could someone provide me with an example on how to access the data in the structure above? Nothing I’ve read addresses such a structure. Also if the example isn’t totally obvious, could you break it down for me? I hate to sound like such a noob, but this is really frustrating me. This is Perl 5.003 BTW (old, old stuff on old, old machines).

You’ll have to dereference $flints, which is a scalar, in order to get at its underlying array. Think of the reference as being a pointer to something. To access an individual element you have to say something like $flints->[0]. To get the array size you have to tell perl that it’s actually an array, e.g., scalar @{ $flints }.

You can also do this: $$flints[0], which makes sense to my twisted mind because:

@array is to $array[0] as @{flints} is to {$flints}[0], or $$flints[0] for short.

In short, what they said. But in an attempt to be helpful:

$flints[0] = the zero element of array @flints
$flints->[0] = the zero element of the array referenced by $flints

** Balthisar**, the best document for this sort of thing is the Perl data-structure cookbook.

Also, the best place to ask Perl questions is Perl Monks. You will find that your favorite programmer is very active there.

Ah! It was the curly brackets that did the trick for me, coupled with the arrow notation. I don’t know why I thought changing the symbol would do the dereferencing for me. That is, I’d been thinking that
@variable[n] was the same as @ {$variable->[n]} which clearly isn’t the case. Seeing it in this fashioned has totally changed things for me. So thanks to Terminus Est for getting there first. Jawdirk, doing $$ gives me compiler errors, but part of the problem is I have an ancient version of Perl 5.003 on a Sun server with broken modules, so I don’t trust that $$ shouldn’t work or it’s another error. Not to exclude duality from the accolades for helping. And finally friedo, that’s a lot clearer than the official Perl documentation I stumbled across, but seeing where it’s hosted, I wonder why there’s duplicate documentation. I guess there are multiple ways to document Perl, too. :wink:

And, technically, shouldn’t your data be:



       flintstones        => [ "fred",   "wilma", "pebbles"  ],
       rubbles            => [ "barney", "betty", "bamm-bamm"]