PERL - printing a hash of hashes to screen

Hi there

I have a hash of hashes made up of the following data

bge0|100|half|10.36.100.21
bge1|1000|full|10.36.100.22
bge2|1000|full|10.36.100.23

which when i turn into a hash, would look like this inside the system

bge0 ->
           nic_speed -> 100
           nic_duplex -> half
           nic_ip -> 10.36.100.21
bge1 ->
           nic_speed -> 1000
           nic_duplex -> full
           nic_ip -> 10.36.100.22
bge2 ->
           nic_speed -> 1000
           nic_duplex -> full
           nic_ip -> 10.36.100.23

but there doesnt seem to be an obvious way of using the print command to just print this all to the screen so i can see whats going on inside the hash. i just have to trust its worked...I found this snippet of code which seems to work on a single hash, but not on a "hash of hashes" like mine ...

  while ( ($key,$value) = each %hServerData ) {
    print "$key => $value\n";
  }

I get the following when I run it

$ ./tester.pl
bge0 => HASH(0x88dafc8)
bge1 => HASH(0x88daf28)
bge2=> HASH(0x88db018)

so its printing the value of the hash and finding that its another hash, hence the "HASH(0x88db018)" stuff

Does anybody know how i would be able to adjust the above snippet of code so that it printed something similar to the layout defined above with the top level hash AND the key value pairs of the sub hash?

any help on this would be greatly appreciated

You can try something like this:

perl  -E'
%hServerData = (
    bge0 => {
        nic_speed  => 100,
        nic_duplex => "half",
        nic_ip     => "10.36.100.21"
    },
    bge1 => {
        nic_speed  => 1000,
        nic_duplex => "full",
        nic_ip     => "10.36.100.22"
    },
    bge2 => {
        nic_speed  => 1000,
        nic_duplex => "full",
        nic_ip     => "10.36.100.23"
    }
);
while ( my ( $key, $value ) = each %hServerData ) {
    say $key, " ->";
    while ( my ( $key, $value ) = each %$value ) {
        say "\t", $key, " -> ", $value;
    }
}'

output:

zsh-4.3.10[t]% perl  -E'
%hServerData = (
    bge0 => {
        nic_speed  => 100,
        nic_duplex => "half",
        nic_ip     => "10.36.100.21"
    },
    bge1 => {
        nic_speed  => 1000,
        nic_duplex => "full",
        nic_ip     => "10.36.100.22"
    },
    bge2 => {
        nic_speed  => 1000,
        nic_duplex => "full",
        nic_ip     => "10.36.100.23"
    }
);
while ( my ( $key, $value ) = each %hServerData ) {
    say $key, " ->";
    while ( my ( $key, $value ) = each %$value ) {
        say "\t", $key, " -> ", $value;
    }
}'
bge1 ->
        nic_ip -> 10.36.100.22
        nic_speed -> 1000
        nic_duplex -> full
bge0 ->
        nic_ip -> 10.36.100.21
        nic_speed -> 100
        nic_duplex -> half
bge2 ->
        nic_ip -> 10.36.100.23
        nic_speed -> 1000
        nic_duplex -> full

Or use DataDumper:

zsh-4.3.10[t]% perl -MData::Dumper -E'
   %hServerData = (
    bge0 => {
        nic_speed  => 100,
        nic_duplex => "half",
        nic_ip     => "10.36.100.21"
    },
    bge1 => {
        nic_speed  => 1000,
        nic_duplex => "full",
        nic_ip     => "10.36.100.22"
    },
    bge2 => {
        nic_speed  => 1000,
        nic_duplex => "full",
        nic_ip     => "10.36.100.23"
    }
);
say Dumper \%hServerData
  '
$VAR1 = {
          'bge1' => {
                      'nic_ip' => '10.36.100.22',
                      'nic_speed' => 1000,
                      'nic_duplex' => 'full'
                    },
          'bge0' => {
                      'nic_ip' => '10.36.100.21',
                      'nic_speed' => 100,
                      'nic_duplex' => 'half'
                    },
          'bge2' => {
                      'nic_ip' => '10.36.100.23',
                      'nic_speed' => 1000,
                      'nic_duplex' => 'full'
                    }
        };

thanks for the reply radoulov, i much prefer the method whereby i dont install any extra modules, so when running your first example (even with no "use strict;" defined) i get the following

Unquoted string "say" may clash with future reserved word at ./CMDB-tester.pl line 39.
String found where operator expected at ./tester.pl line 39, near "say "\t""
        (Do you need to predeclare say?)
syntax error at ./tester.pl line 39, near "say "\t""
Execution of ./tester.pl aborted due to compilation errors.

Excuse my lack of perl knowledge here, but is there something i have to do to get the "say" keyword to work ???

thanks again

Use Perl 5.10 (and activate the new features). Use print instead of say :slight_smile:

By the way, Data: :D umper isn't a new module, but has been a part of the core distribution for quite some time now. It has the advantage that you can write a quick store/restore part using it and eval.

thanks guys .... it works ..i used

   while ( my ( $key, $value ) = each %hServerData ) {
     print $key, " ->\n";
    while ( my ( $key, $value ) = each %$value ) {
        print "\t", $key, " -> ", $value, "\n";
    }
   }

I tried putting it in a function/subroutine and calling it, which didnt work .. but hey, thats for another day and a bit beyond my current perl knowledge

thanks again

open FH,"<a";
while(<FH>){
chomp;
my @tmp=split("[|]",$_);
$hash{$tmp[0]}->{speed}=$tmp[1];
$hash{$tmp[0]}->{duplix}=$tmp[2];
$hash{$tmp[0]}->{ip}=$tmp[3];
}
foreach my $key(keys %hash){
 print $key,"->\n";
 foreach my $k(keys %{$hash{$key}}){
  print "       $k->$hash{$key}->{$k}\n";
 }
}