how to print hash within hash

M

mike

hi
i have hash like this

my %hash = ( a => { 1=>1,2=>2, 3=>3,4=>4, 5=>5} ,
b => { 1=>1,2=>2, 3=>3,4=>4, 5=>5},
c => { 1=>1,2=>2, 3=>3,4=>4, 5=>5},
d => { 1=>1,2=>2, 3=>3,4=>4, 5=>5 );


how can i print this hash that has hashes within it.?
thanks
 
K

ko

mike said:
hi
i have hash like this

my %hash = ( a => { 1=>1,2=>2, 3=>3,4=>4, 5=>5} ,
b => { 1=>1,2=>2, 3=>3,4=>4, 5=>5},
c => { 1=>1,2=>2, 3=>3,4=>4, 5=>5},
d => { 1=>1,2=>2, 3=>3,4=>4, 5=>5 );

----------------------------------------------^^^

missing curly bracket
how can i print this hash that has hashes within it.?
thanks

One way to do it:

while ( my ($key, $href) = each %hash) {
print "Key: $key\n";
while ( my ($href_key, $href_value) = each %$href) {
print "\t$href_key => $href_value\n";
}
}

If you want to sort your data structure, you'll need to use a foreach
loop instead. 'perldoc perldsc' for details - specifically the section
'Access and Printing of a HASH OF HASHES'

HTH - keith
 
A

Andy Baxter

At earth time Wed, 31 Dec 2003 21:54:53 -0800, the following transmission
was received from the entity known as mike:
hi
i have hash like this

my %hash = ( a => { 1=>1,2=>2, 3=>3,4=>4, 5=>5} ,
b => { 1=>1,2=>2, 3=>3,4=>4, 5=>5},
c => { 1=>1,2=>2, 3=>3,4=>4, 5=>5},
d => { 1=>1,2=>2, 3=>3,4=>4, 5=>5 );


how can i print this hash that has hashes within it.?
thanks

Data::Dumper is handy for printing stuff like this just to dump it for
debugging - it will work with pretty much any hashref / arrayref data
structure.
 
G

Gregory Toomey

It was a dark and stormy night, and mike managed to scribble:
hi
i have hash like this

my %hash = ( a => { 1=>1,2=>2, 3=>3,4=>4, 5=>5} ,
b => { 1=>1,2=>2, 3=>3,4=>4, 5=>5},
c => { 1=>1,2=>2, 3=>3,4=>4, 5=>5},
d => { 1=>1,2=>2, 3=>3,4=>4, 5=>5 );


how can i print this hash that has hashes within it.?
thanks

Just whip up the "hash of hashes" recipe in this Perl cookbook:
http://www.perl.com/doc/FMTEYEWTK/pdsc/

gtoomey
 
C

Clyde Ingram

Mike,

mike said:
. . .
how can i print this hash that has hashes within it.?

If you are not worried about the order of listing hash keys, try:

#!/bin/perl -wT
use strict;
use Data::Dumper;
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Indent = 1;

my %hash = (
a => { 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 },
b => { 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 },
c => { 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 },
d => { 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 }
);

print Data::Dumper->Dump( [ \%hash ], [ "%hash" ] ) . "\n";


Changing the local copies of the Terse and Indent variables (according to
perldoc Data::Dumper) changes the layout. But I get:

{
'c' => {
'4' => 4,
'1' => 1,
'3' => 3,
'2' => 2,
'5' => 5
},
'a' => {
'4' => 4,
'1' => 1,
'3' => 3,
'2' => 2,
'5' => 5
},
'b' => {
'4' => 4,
'1' => 1,
'3' => 3,
'2' => 2,
'5' => 5
},
'd' => {
'4' => 4,
'1' => 1,
'3' => 3,
'2' => 2,
'5' => 5
}
}

Happy New Year,
Clyde
 
@

@(none)

Hi,
u can try:

foreach $element ( sort keys %hash) {
for $j ( sort keys %{$hash{$element}} ) {
print "first element=$element \n";
print "this j=$j $hash{$element}{$j} \n";
}
}

Hope this will do the trick ?

:) Ketil
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top