Hi,
I know how to stor multiple values in has table with single key.Basically HOH
But i am not able to do the same when the hash has multiple keys

I am trying something like this and when i try to print it gives garbage value for the has keys..

my $referenceTable
foreach (@port) {
            undef $referenceTable->{$key1}->{$key2}->{$_}
        }
        my ($k,$j,$v);

        while (($k,$j,$v) = each(%{$referenceTable})) {
            print "Hash key is $k and $j  \n";
            print "Values stored in the hash for $k are:\n";
            foreach (keys %{$v}) {
                print "$_\n";
            }
        print "\n"

Any idea what I am doing wrong. Or any suggestions on how to do HOH with multiple keys?

Thanks
R

Recommended Answers

All 5 Replies

I don't know how to print what you want yet but to figure it out I might start by using Data::Dumper to print the data structure and values.

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $referenceTable;
my @port = qw(80 70 1083 42);
my ( $key1, $key2 ) = ( 'First Key', 'Second Key' );

foreach (@port) {
    undef $referenceTable->{$key1}->{$key2}->{$_};
}

print Dumper($referenceTable);

Prints the following:

$VAR1 = {
          'First Key' => {
                           'Second Key' => {
                                             '42' => undef,
                                             '70' => undef,
                                             '1083' => undef,
                                             '80' => undef
                                           }
                         }
        };

Thanks dude. Seems like it might work. Will give a shot right away.

However I have couple of questions.
1) You define port as
my @port = qw(80 70 1083 42);
What is qw? I basically parse a file and get port values and push it into @port. Should i assign it as
@port = qw();#in the beginning
and keep pushing data into it.

2) How do I get the values of key1 and key 2 after multiple file parsing to compare the key values later on?

Thanks again for your help.

I used my @port = qw(80 70 1083 42); to build myself an array for testing. qw() puts quotes around the space-delimited items you give it as arguments. The array you build by pushing values into it should work just as well.

As for how to retrieve the keys from the hash after it has been built I had a difficult time with that so may not have found the best way. But, how about the following? I couldn't get it to work at all until I changed your my $referenceTable to my %referenceTable and then slavishly followed one of examples at http://perldoc.perl.org/perldsc.html#HASHES-OF-HASHES.

#!/usr/bin/perl
use strict;
use warnings;

my @port = qw(80 70 1083 42);
my ( $key1, $key2 ) = ( 'First Key', 'Second Key' );
my %referenceTable; #Declare a hash, not a reference.

foreach (@port) {
    undef $referenceTable{$key1}{$key2}{$_};
}

foreach my $x ( keys %referenceTable ) {
    print "Hash key is $x and ";
    for my $y ( keys %{ $referenceTable{$x} } ) {
        print "$y \n";
        print "Keys for hash stored in this hash are:\n";
        for my $z ( keys %{ $referenceTable{$x}{$y} }){
            print "$z\n";
        }
    }
}

This gives the following output:

Hash key is First Key and Second Key 
Keys for hash stored in this hash are:
42
70
1083
80

This is awesome.
It works for the first time.
However I am having some issues when I go over my parsed file. I get Key1 and Key2 and port values. Then i put it in my %referenceTable.

Then I continue parsing get new key2 and try to put it in my %referenceTable but my hash key2 values dont change when I try to print it. It gives me old values, even though before continuing to parse I assign $key2 = 0 and find new $key2. My $key1 values dont change in one file so I dont assign that to 0.

So for second interation my values will be like

#@port =42
#key1 = First Key
#key2 = NEW_Second_Key

But when I print for the second iteration my output is like this

Hash Key is First Key and OLD_Second_Key
Keys for hash stored in this hash are:
42
NEW_SECOND_KEY
Keys for hash stored in this hash are:
42

It prints the hash elements twice.

Why do we assign it as
undef $referenceTable{key1}{key2}{$_}

What does undef do?? I tried to assign values to $referenceTable without undef but then it give me some error message like this
Useless use of hash element in void context at ..

And my hash table is empty.


Sorry to keep bugging you. Newbie to perl and still learning. Really appreciate all your help.

Hi d5e5,

I figured it out. Don't bother look into it. Also undef is explained well on the link you provided.

Just for ppl doing google,
Its a for loop and print's all keys and elements. That's why i got confused. But the code above works perfect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.