codingguy3000
codingguy3000

Reputation: 2835

In Perl how do sub hash tables work?

I'm working on some Perl code and I'm trying to gain understanding as to what Perl is doing.

I'm stuck on the following hash table code:

$summary01{$myHash{'ConfigID'}}{'ConfigID'} = $myHash{'ConfigID'}; 

The variable $myHash contains a single database record.

This code is setting storing a summary of data from a sql query.

can someone explain what the }}{ code is doing? Is this a multiple dimension hash table?

Thanks,

Upvotes: 0

Views: 66

Answers (2)

CodeGorilla
CodeGorilla

Reputation: 913

Lets break $summary01{$myHash{'ConfigID'}}{'ConfigID'} = $myHash{'ConfigID'}; down into something more long-winded:

# Observation: %$summary01 is a hash of hashes
my $foo = $myHash{'ConfigID'};  # $foo is a scalar value
my $bar = $summary01{$foo};     # $bar is a pointer, a copy of a reference
$bar{'ConfigID'} = $foo;        # .... and this is the actual assignment

Upvotes: 1

amon
amon

Reputation: 57640

The code

$summary01{$myHash{'ConfigID'}}{'ConfigID'} = $myHash{'ConfigID'}; 

can be rewritten as

my $configID = $myHash{ConfigID};
$summary01{$configID}{ConfigID} = $configID;

I.e. assuming the $configID = 123, this would add an entry to %summary01 like

%summary01 = (
    ...,
    123 => { ..., ConfigID => 123 },
    ...,
);

The }}{ sequence has no special meaning. This is just a lookup in a nested hash $summary{ ... }{ ... } where one of the hash keys is another hash lookup $myHash{ConfigID}.

Upvotes: 5

Related Questions