user1157751
user1157751

Reputation: 2457

Perl - Hash Table and foreach statement

I'm a bit confused on perl hashes.

I know that you can create a hash by calling:

my %hashTable;

$hashTable("Key") = "Value"

later on, if you want to retrieve the value, you can do

print $hashTable("Key")

However, I'm confused on this for loop

foreach (keys %{$hashTable{"key"}})
{
    print $_
}

without the %{$hashTable{"key"}}, it looks that it is going to print out each of the keys, but what happens if you do a % in front of a hash table?

I tried to test out this function, by giving it a key called "key", it would give me an error: can't use string ("key") as a hash reference. Is there something to do with multi-dimensional hash tables?

Upvotes: 0

Views: 675

Answers (2)

AntonH
AntonH

Reputation: 6437

What's happening is that you have a hash, called %hashTable. It has a value with the key key, which has for a value another hash table.

So the code:

foreach (keys %{$hashTable{"key"}})
{
    print $_
}

is looping through that second hash, and printing out the keys that it contains.

Here is an example using the documentation link that Miller provided:

#!/usr/bin/perl

use strict;
use warnings;

my %HoH = (
    flintstones => {
        lead => "fred",
        pal => "barney",
    },
    jetsons => {
        lead => "george",
        wife => "jane",
        "his boy" => "elroy",
    },
    simpsons => {
        lead => "homer",
        wife => "marge",
        kid => "bart",
    },
);

foreach (keys %{$HoH{"flintstones"}})
{
    print $_."\n";
}

This has the output:

lead
pal

Upvotes: 4

clt60
clt60

Reputation: 63972

Imagine your hashTable as

key1:           <-+
    value1        |
key2:             |
    value2        += this is your hashTable, with 3x key/value
key3:             |
    value3      <-+

Now chaange the value for the keyN - so, it will not contain a scalar value, but one another hash, like:

key1:
    subkey1a: subval1a      <-+ this "HASH" is the value for the "key1".
    subkey1b: subval1b      <-|
key2:
    subkey2a: subval2a
key3:
    subkey3a: subval3a
    subkey3b: subval3b
    subkey3c: subval3c

Upvotes: 1

Related Questions