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

I was reading the docs for Finance::Quote and saw some weird syntax described as "two-dimensional hashes." I thought, "That must just be a hash of hashes," and, "I had no idea that syntax would work." Messing around with it I immediately discovered it wasn't an alternative syntax but an entirely different key construction.

In 10 years of Perl I've never seen this and I realize I might be naïve but does anyone have an overview or insight into this? Is this what they're usually called? Are these used in the wild? How long have they been available? Are they in a major document that I've failed to FR?

DB<15>use Finance::Quote; DB<16>$q = Finance::Quote->new; DB<17>%quotes = $q->fetch("nasdaq","lway"); DB<18> use YAML; DB<19> print Dump \%quotes --- "LWAY\x1cask": ~ "LWAY\x1cavg_vol": 19988 "LWAY\x1cbid": 9.05 "LWAY\x1ccap": 208.6M "LWAY\x1cclose": 12.55 "LWAY\x1ccurrency": USD "LWAY\x1cdate": 06/19/2009 "LWAY\x1cday_range": '12.2901 - 12.75' "LWAY\x1cdiv": 0.00 "LWAY\x1cdiv_date": ~ "LWAY\x1cdiv_yield": ~ "LWAY\x1ceps": 0.153 "LWAY\x1cex_div": ' 9-Mar-04' "LWAY\x1chigh": 12.75 "LWAY\x1cisodate": 2009-06-19 "LWAY\x1clast": 12.40 "LWAY\x1clow": 12.2901 "LWAY\x1cname": 'Lifeway Foods, In' "LWAY\x1cnet": -0.15 "LWAY\x1copen": 12.66 "LWAY\x1cp_change": -1.20 "LWAY\x1cpe": 82.03 "LWAY\x1cprice": 12.40 "LWAY\x1csuccess": 1 "LWAY\x1csymbol": LWAY "LWAY\x1ctime": 16:00 "LWAY\x1cvolume": 29649 "LWAY\x1cyear_range": '5.43 - 15.48' DB<20> print $quotes{"LWAY","open"} 12.66 DB<23> $asdf{qw(one two three )} = "OH HAI"; DB<24> print $asdf{"one","two","three"} OH HAI

Replies are listed 'Best First'.
Re: Um... WTF? Multi-dimensional hashes
by GrandFather (Saint) on Jun 22, 2009 at 01:17 UTC

    The technique is documented in perlvar in the description of the $; special variable. Probably better done as $quotes{LWAY}{open} however (which is not a substitute syntax btw!).


    True laziness is hard work
Re: Um... WTF? Multi-dimensional hashes
by gwadej (Chaplain) on Jun 22, 2009 at 01:21 UTC

    This feature allowed pseudo-multi-dimensional arrays before Perl 5 and references. I remember using them once or twice in Perl 4.

    G. Wade
Re: Um... WTF? Multi-dimensional hashes
by Anonymous Monk on Jun 22, 2009 at 02:24 UTC
    Exsqueeze me, but "WTF?" My mother never talked like that :)

      It's short for "Why the Following?" Isn't it? Just like "Read the Friendly Manual." Hackers are the most polite and cultured persons in the world.

        Cultured? Nay, fermented.
Re: Um... WTF? Multi-dimensional hashes
by zappepcs (Acolyte) on Jun 22, 2009 at 03:09 UTC
    I've never seen any particular documentation, but picked up in an example when looking for a hash of hashes. Hashes of arrays are useful for columnar data, but hashes of hashes allow you to keep a well ordered structure of data and pass it around like a football in your code if needed. As an example, say you want a hash of contacts data. Calling if exists on $contacts{$last}{$first} will tell you if you have that person in the contacts hash, so you can then look for other pertinent details. With the hash of hashes you have a hash for each person like $contacts{$last}{$first}{HomePhone}=
    $contacts{$last}{$first}{WorkPhone}=
    $contacts{$last}{$first}{HomeEmail}=
    $contacts{$last}{$first}{WorkEmail}=
    or any of the several ways to assign data to a hash value Accessing and assigning values can be a bit of a mind warp, but once you understand it, it is very useful and compact. see http://docstore.mik.ua/orelly/perl/prog3/ch09_04.htm for a bit more. I've been using such since 5.8 but am not sure how long it's been available.

      Well, that actually nothing to do with the question. And it's not good to link to pirated copies of books. :(