Hashes of hashes?

A

asincero

I have this:

use strict;
my %my_hash = ();

$my_hash{'1'}{'a'} = "Hello";
$my_hash{'1'}{'b'} = "world!";
$my_hash{'2'}{'c'} = "Foo";
$my_hash{'2'}{'d'} = "bar";

And I want to do something like this:

foreach my $k (keys $my_hash{'1'})
{
print "$k\n";
}

Should give the output of:
a
b

But of course the above code is wrong. How do I correct it?

Thanks.

-- Arcadio
 
L

Lars Madsen

asincero said:
I have this:

use strict;
my %my_hash = ();

$my_hash{'1'}{'a'} = "Hello";
$my_hash{'1'}{'b'} = "world!";
$my_hash{'2'}{'c'} = "Foo";
$my_hash{'2'}{'d'} = "bar";

And I want to do something like this:

foreach my $k (keys $my_hash{'1'})
{
print "$k\n";
}

Should give the output of:
a
b

But of course the above code is wrong. How do I correct it?

Thanks.

-- Arcadio

do what the compiler tells you

use %{ $my_hash{'1'} }

--

/daleif (remove RTFSIGNATURE from email address)

LaTeX FAQ: http://www.tex.ac.uk/faq
LaTeX book: http://www.imf.au.dk/system/latex/bog/ (in Danish)
Remember to post minimal examples, see URL below
http://www.tex.ac.uk/cgi-bin/texfaq2html?label=minxampl
 
D

David Squire

asincero said:
I have this:

use strict;
my %my_hash = ();

$my_hash{'1'}{'a'} = "Hello";
$my_hash{'1'}{'b'} = "world!";
$my_hash{'2'}{'c'} = "Foo";
$my_hash{'2'}{'d'} = "bar";

And I want to do something like this:

foreach my $k (keys $my_hash{'1'})
{
print "$k\n";
}

Should give the output of:
a
b

But of course the above code is wrong. How do I correct it?

What does Perl tell you? The error message is:

Type of arg 1 to keys must be hash (not hash element) at ./test.pl line
13, near "})
"

So, Perl is telling you what the problem is. $my_hash{'1'} is a hash
element, which in this case stores a *reference* to a hash (see perldoc
perldsc). You need to dereference it. What you want is:

----

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

my %my_hash = ();

$my_hash{'1'}{'a'} = "Hello";
$my_hash{'1'}{'b'} = "world!";
$my_hash{'2'}{'c'} = "Foo";
$my_hash{'2'}{'d'} = "bar";

foreach my $k (keys %{$my_hash{'1'}})
{
print "$k\n";
}

----

Output:

a
b


DS
 
A

asincero

Thanks guys. I guess I didn't understand what Perl was trying to tell
me.

-- Arcadio
 
D

Dave Weaver

I have this:

use strict;
my %my_hash = ();

$my_hash{'1'}{'a'} = "Hello";
$my_hash{'1'}{'b'} = "world!";
$my_hash{'2'}{'c'} = "Foo";
$my_hash{'2'}{'d'} = "bar";

Others have already answered your question, but as an observation, if
you're populating a hash with static data, you may find it more
readable to lay your code out something like this:

my %my_hash = (
1 => {
a => 'Hello',
b => 'world!',
},
2 => {
c => 'Foo',
d => 'bar',
},
);

which has the same effect as your code above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top