# Perl data structure reference question

**URL:** <https://boards.straightdope.com/t/perl-data-structure-reference-question/347274>\
**Category:** Factual Questions\
**Created:** [March 6, 2006, 6:12pm UTC](https://boards.straightdope.com/t/perl-data-structure-reference-question/347274 "2006-03-06T18:12:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Balthisar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/balthisar/32/2825_2.png) [@Balthisar](https://boards.straightdope.com/u/Balthisar)\
**Post date:** [March 6, 2006, 6:12pm UTC](https://boards.straightdope.com/t/perl-data-structure-reference-question/347274/1 "2006-03-06T18:12:31Z")

</div>

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:

```auto

%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:

```auto

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

```auto

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).

---

<div class="post-metadata">

**Author:** ![Terminus\_Est](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/terminus_est/32/3087_2.png) [@Terminus\_Est](https://boards.straightdope.com/u/Terminus_Est)\
**Post date:** [March 6, 2006, 6:41pm UTC](https://boards.straightdope.com/t/perl-data-structure-reference-question/347274/2 "2006-03-06T18:41:51Z")

</div>

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 }.

---

<div class="post-metadata">

**Author:** ![jawdirk](https://avatars.discourse-cdn.com/v4/letter/j/df705f/32.png) [@jawdirk](https://boards.straightdope.com/u/jawdirk)\
**Post date:** [March 6, 2006, 7:02pm UTC](https://boards.straightdope.com/t/perl-data-structure-reference-question/347274/3 "2006-03-06T19:02:49Z")

</div>

> [@Terminus Est](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![duality72](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/duality72/32/3910_2.png) [@duality72](https://boards.straightdope.com/u/duality72)\
**Post date:** [March 6, 2006, 11:27pm UTC](https://boards.straightdope.com/t/perl-data-structure-reference-question/347274/4 "2006-03-06T23:27:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [March 7, 2006, 12:11am UTC](https://boards.straightdope.com/t/perl-data-structure-reference-question/347274/5 "2006-03-07T00:11:16Z")

</div>

\*\* Balthisar\*\*, the best document for this sort of thing is the [Perl data-structure cookbook](http://perldoc.perl.org/perldsc.html).

Also, the best place to ask Perl questions is [Perl Monks](http://www.perlmonks.org/). You will find that [your favorite programmer](http://www.perlmonks.org/?node=friedo) is very active there.

---

<div class="post-metadata">

**Author:** ![Balthisar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/balthisar/32/2825_2.png) [@Balthisar](https://boards.straightdope.com/u/Balthisar)\
**Post date:** [March 7, 2006, 12:32am UTC](https://boards.straightdope.com/t/perl-data-structure-reference-question/347274/6 "2006-03-07T00:32:20Z")

</div>

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. 😉

---

<div class="post-metadata">

**Author:** ![Tim\_T-Bonham.net](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@Tim\_T-Bonham.net](https://boards.straightdope.com/u/Tim_T-Bonham.net)\
**Post date:** [March 7, 2006, 5:35am UTC](https://boards.straightdope.com/t/perl-data-structure-reference-question/347274/7 "2006-03-07T05:35:38Z")

</div>

And, technically, shouldn’t your data be:

```auto

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

```
