470,811 Members | 1,181 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

references

I am studying the examples in the book "Perl 5 by Examples". Here's a
short program from that book:

my $dbf2 = { 3 => { 4=>5, 5=>6 } };
my($key1,$value1,$key2,$value2);
while (($key1,$value1) = each(%{$dbf2})) {
print("level 1: $key1 => $value1\n");
while(($key2,$value2) = each(%{$value1})) {
print("level 2: $key2 => $value2\n");
}
}

The program would prints:

Level 1: 3 => HASH(123456)
Level 2: 4 => 5
Level 2: 5 => 6

Now, what if I create this (which passed the compilation):

my $dbf3 = { { 4=>5, 5=>6} }

How could I make the same program work?

--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 4:54pm up 2 days 18:48 load average: 1.00 1.00 1.00
Jul 19 '05 #1
2 1363
toylet wrote:
I am studying the examples in the book "Perl 5 by Examples". Here's a
short program from that book:

my $dbf2 = { 3 => { 4=>5, 5=>6 } };
my($key1,$value1,$key2,$value2);
while (($key1,$value1) = each(%{$dbf2})) {
print("level 1: $key1 => $value1\n");
while(($key2,$value2) = each(%{$value1})) {
print("level 2: $key2 => $value2\n");
}
}

The program would prints:

Level 1: 3 => HASH(123456)
Level 2: 4 => 5
Level 2: 5 => 6

Now, what if I create this (which passed the compilation):

my $dbf3 = { { 4=>5, 5=>6} }

How could I make the same program work?

What are you trying to accomplish? In $dbf2 you have a hash that has
one key/value pair. Key name "3" that has a value that is another hash
has 2 key/value pairs. When you defined $dbf3 you have defined a hash
that has a single key/value pair the key is a hash and the value is
undefined.

To better see what data structures you have created try using
Data::Dumper module.
Here is what Dumper gives you for you two hashes:
(This is $dbf2)
$VAR1 = {
'3' => {
'4' => 5,
'5' => 6
}
};

(This is $dbf3)
$VAR1 = {
'HASH(0x8260d54)' => undef
};


--
Thanks
Charles LaCour
Jul 19 '05 #2
thank you for the tip on Data::Dumper. I was just testing my skills on
accessing data structures of Perl.
To better see what data structures you have created try using
Data::Dumper module.
Here is what Dumper gives you for you two hashes:
(This is $dbf3)
$VAR1 = {
'HASH(0x8260d54)' => undef
};


--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 10:50am up 23 min 0 users 1.12 1.07
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Tom | last post: by
22 posts views Thread by xmp333 | last post: by
33 posts views Thread by JKop | last post: by
11 posts views Thread by codebloatation | last post: by
14 posts views Thread by el_sid | last post: by
30 posts views Thread by jeremygetsmail | last post: by
3 posts views Thread by DonJefe | last post: by
9 posts views Thread by igor.kulkin | last post: by
3 posts views Thread by CenturionX | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.