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

I'd like to use a hash like this:
my $rboc = "SA"; my $telco = "0009"; $HoA{$rboc}{$telco} ++;
or perhaps $HoA{$rboc,$telco} ++; I've never seen this in any literature but it seems to work OK...comments?

Replies are listed 'Best First'.
Re: multi-dimensional hash
by ikegami (Patriarch) on Nov 02, 2005 at 20:57 UTC

    $HoA{$rboc}{$telco}++; is fine, except it's a HoH, not a HoA. It's a shortcut for

    $HoA{$rboc} = {} unless defined $HoA{$rboc}; my $anon_temp = $HoA{$rboc}; $anon_temp->{$telco}++

    $HoA{$rboc, $telco}++;
    also works, but it means
    $HoA{join($;, $rboc, $telco)}++;
    which is probably not what you want.

    Update: s/unless exists/unless defined/

Re: multi-dimensional hash
by GrandFather (Saint) on Nov 02, 2005 at 21:00 UTC

    That's fine, but it is a Hash of Hash (HoH), not a Hash of Array (HoA). You should perhaps take a look at Data::Dumper.

    use strict; use warnings; use Data::Dumper; my $rboc = "SA"; my $telco = "0009"; my %HoH; my %MultiHash; $HoH{$rboc}{$telco} ++; $MultiHash{$rboc, $telco} ++; print Dumper (\%HoH); print Dumper (\%MultiHash);

    Prints:

    $VAR1 = { 'SA' => { '0009' => 1 } }; $VAR1 = { 'SA0009' => 1 };

    Note that there is a hidden character in the 'SA0009' string!

    Update: added second method.


    Perl is Huffman encoded by design.
      thanks to all who answered so QUICKLY! the HoA business was carelessness...sorry. regards, Barry
Re: multi-dimensional hash
by davido (Cardinal) on Nov 02, 2005 at 20:59 UTC

    The only comment is that it's not a HoA, it's a HoH.

    The datastructure will look like this:

    %HoH = ( SA => { 0009 => 1 } );

    It's done that way all the time. Your second method: $HoA{$robc,$telco} will work but not in the same way (ie, it's not a hash of hashes anymore). IMHO, it's probably better to use a HoH instead of a hash with some multi-part key.


    Dave

Re: multi-dimensional hash
by kirbyk (Friar) on Nov 02, 2005 at 20:56 UTC
    The first way you do it should work fine. It's really a hash of hashes (So, the variable name HoA should probably be HoH, or really probably something more descriptive.)

    -- Kirby, WhitePages.com

Re: multi-dimensional hash
by blue_cowdawg (Monsignor) on Nov 02, 2005 at 21:01 UTC
        or perhaps $HoA{$rboc,$telco} ++; I've never seen this in any literature but it seems to work OK...comments?

    My first thought is "what are you trying to accomplish? "

    Consider the following:

    #!/usr/bin/perl -w ###################################################################### +## use strict; use Data::Dumper; my %hash=(); foreach my $i('a'...'g'){ foreach my $j('a'..'g'){ $hash{$i,$j}="-"; } } print Dumper(%hash);

    When run produces:

    Was this what you were after?

    I noticed that GrandFather showed the other method. It is all in what you want to accomplish...


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      The data will be organized betted if you substitue
      print Dumper(%hash);
      with
      print Dumper(\%hash);

            The data will be organized betted if you substitue

        True

        Done that way it looks like:

        $VAR1 = { 'dd' => '-', 'fb' => '-', 'ab' => '-', 'ge' => '-', 'bd' => '-', 'dc' => '-', 'aa' => '-', 'cc' => '-', 'ae' => '-', 'ba' => '-', 'cb' => '-', 'gb' => '-', 'ef' => '-', 'ea' => '-', 'gd' => '-', 'ec' => '-', 'cg' => '-', 'ad' => '-', 'eg' => '-', 'de' => '-', 'fe' => '-', 'fg' => '-', 'dg' => '-', 'cf' => '-', 'bf' => '-', 'ed' => '-', 'ce' => '-', 'ac' => '-', 'gf' => '-', 'bb' => '-', 'db' => '-', 'ee' => '-', 'bc' => '-', 'ga' => '-', 'gc' => '-', 'af' => '-', 'fd' => '-', 'gg' => '-', 'fc' => '-', 'ca' => '-', 'ag' => '-', 'be' => '-', 'fa' => '-', 'cd' => '-', 'ff' => '-', 'bg' => '-', 'df' => '-', 'da' => '-', 'eb' => '-' };

        Much more readable.

Re: multi-dimensional hash
by sauoq (Abbot) on Nov 02, 2005 at 21:49 UTC
    I've never seen this in any literature

    Then you have yet to read perldoc perldsc the "Perl Data Structures Cookbook". If you are just venturing into using more complicated data structures, this would probably be a really good time to check it out.

    -sauoq
    "My two cents aren't worth a dime.";