Printing Hash of Array of Arrays

D

Deepu

Hi All,

How to print hash of array of arrays without using Data::Dumper.

I am trying to print

%hash = (
FILE1 => [
[
STATE1
STATE2
STATE3
],
[
STATEX
STATEA
STATE4
]
],

FILE2 => [
[
STATE2
STATE5
],

and so on.

I tried:

foreach my $key (sort keys %hash) {
print "$key\n"; ## - It prints the file names correctly as keys

for $i (0 .. $#{$hash{$key}}) {
print "$i: $hash{$key}[$i]";
}
}

But now i get output like:

0: ARRAY(0x5fdbf0)
and so on.

Please help me on this.

Thanks
Deep
 
P

Paul Lalli

Deepu said:
How to print hash of array of arrays without using Data::Dumper.

I am trying to print

%hash = (
FILE1 => [
[
STATE1
STATE2
STATE3
],
[
STATEX
STATEA
STATE4
]
],

FILE2 => [
[
STATE2
STATE5
],

and so on.

I tried:

foreach my $key (sort keys %hash) {
print "$key\n"; ## - It prints the file names correctly as keys

for $i (0 .. $#{$hash{$key}}) {
print "$i: $hash{$key}[$i]";
}
}

You said you have a hash of arrays of arrays. So %hash is the hash,
$hash{$key} is a reference to the first-level array, and
$hash{$key}[$i] is a reference to the second level array. You need to
dereference this and loop through the results, just as you did for the
first level:

foreach my $key (sort keys %hash) {
print "$key\n";
for my $i (0 .. $#{$hash{$key}) {
print " Array $i\n";
foreach my $elem (@{$hash{$key}[$i]}) {
print " $elem\n";
}
}
}

Note that the above syntax is just to demonstrate how to do it
following your lead. Were I to write this, I would probably not use a
loop for the final array level, and just print all the array elements
directly:
foreach my $key (sort keys %hash) {
print "$key\n";
for my $i (0 .. $#{$hash{$key}) {
print " Array $i: @{$hash{$key}[$i]}\n";
}
}

Hope this helps,
Paul Lalli
 
I

it_says_BALLS_on_your forehead

Deepu said:
Hi All,

How to print hash of array of arrays without using Data::Dumper.

I am trying to print

%hash = (
FILE1 => [
[
STATE1
STATE2
STATE3
],
[
STATEX
STATEA
STATE4
]
],

FILE2 => [
[
STATE2
STATE5
],

and so on.

I tried:

foreach my $key (sort keys %hash) {
print "$key\n"; ## - It prints the file names correctly as keys

for $i (0 .. $#{$hash{$key}}) {
print "$i: $hash{$key}[$i]";
}
}

But now i get output like:

0: ARRAY(0x5fdbf0)
and so on.


use Data::Dumper;

print Dumper( \%hash );
 
J

John W. Krahn

Deepu said:
How to print hash of array of arrays without using Data::Dumper.

I am trying to print

%hash = (
FILE1 => [
[
STATE1
STATE2
STATE3
],
[
STATEX
STATEA
STATE4
]
],

FILE2 => [
[
STATE2
STATE5
],

and so on.

I tried:

foreach my $key (sort keys %hash) {
print "$key\n"; ## - It prints the file names correctly as keys

for $i (0 .. $#{$hash{$key}}) {
print "$i: $hash{$key}[$i]";
}
}

But now i get output like:

0: ARRAY(0x5fdbf0)
and so on.

You have to dereference the array reference:

for my $key ( sort keys %hash ) {
print "$key\n";
for my $array_ref ( @{ $hash{ $key } } ) {
print "@$array_ref";
}
}



John
 
T

Ted Zlatanov

How to print hash of array of arrays without using Data::Dumper.

It's useful here to classify things into hashes, arrays, and
everything else.

dump_container($hash_of_arrays_of_arrays);

sub dump_container
{
my $ref = shift @_;

if (ref $ref eq 'ARRAY')
{
... do something with @$ref and call dump_container on each element ...
}
elsif (ref $ref eq 'HASH')
{
... do something with %$ref and call dump_container on each value ...
}
else
{
print "Saw element of type ", ref $ref, "and data ", "$ref\n";
}
}

Note that this will handle any combination of container data
structures, as long as they are arrays and hashes. Look at "perldoc
-f ref" for more information.

Avoiding Data::Dumper is weird, but I'm sure you have good reasons...

Ted
 

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

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top