PERL - printing a hash of hashes to screen


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL - printing a hash of hashes to screen
# 1  
Old 08-30-2009
PERL - printing a hash of hashes to screen

Hi there

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

Code:
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

Code:
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 ...


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

I get the following when I run it

Code:
$ ./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

Last edited by hcclnoodles; 08-30-2009 at 10:38 AM..
# 2  
Old 08-30-2009
You can try something like this:

Code:
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:

Code:
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:

Code:
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'
                    }
        };

# 3  
Old 08-30-2009
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

Code:
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
# 4  
Old 08-30-2009
Use Perl 5.10 (and activate the new features). Use print instead of say Smilie
# 5  
Old 08-30-2009
By the way, Data::Dumper 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.
# 6  
Old 08-30-2009
thanks guys .... it works ..i used

Code:
   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
# 7  
Old 08-31-2009
Code:
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";
 }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl hash of hashes anonymous array

Hello experts. I'm having problems with a snippet of code. I was hoping to get help/advice to correct. A file that this script parses has changed to the point where I can no longer use a scalar, it looks as though I need to create an array for a hash of hashes below. The first output of... (1 Reply)
Discussion started by: timj123
1 Replies

2. Shell Programming and Scripting

Perl: Printing null hash values as a " "?

I'm filling in a table of values for grades. I decided to go with reading into a hash from the files but I'm coming up with an error when printing a value that does not exist. I need to know if I can on-the-fly print a space (" ") or blank in place of the grade. Here's what the output should... (2 Replies)
Discussion started by: D2K
2 Replies

3. Homework & Coursework Questions

[solved]Perl: Printing line numbers to matched strings and hashes.

Florida State University, Tallahassee, FL, USA, Dr. Whalley, COP4342 Unix Tools. This program takes much of my previous assignment but adds the functionality of printing the concatenated line numbers found within the input. Sample input from <> operator: Hello World This is hello a sample... (2 Replies)
Discussion started by: D2K
2 Replies

4. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

5. Shell Programming and Scripting

perl: dereferencing a hash of hashes

Hi there, I am trying to dereference my hash of hashes but post dereferencing, it seems to lose its structure I am using Data::dumper to help me anaylise. This is the code im using to build the HoH, (data comes from a file). I have also performed a Dumper on the data structure before and after... (1 Reply)
Discussion started by: rethink
1 Replies

6. Shell Programming and Scripting

hash of hashes : how to print reference and its internal structure?

#use perl 5.8.5; my %h1=(a=>'b', c=>'d'); my %h2=(a1=>'b1', c1=>'d1'); my $R1=\%h1; my $R2=\%h2; my %h= {$R1, $R2}; my $href=\%h; # hash of hashes foreach my $key (keys %$href){ print "Z::$$href{$key}\n" } When I am trying to print elements of hash of hashes, it prints HASH... (1 Reply)
Discussion started by: shristi
1 Replies

7. Shell Programming and Scripting

PERL - another quick hash of hashes question

Hi, sorry, two hash related questions in one day .. but this has got me a bit stuck. I have a mysql database table that kind of looks like this, the table is called "view1" and a snippet of that table (SELECT'ing just rows with serial number 0629AN1200) is below serial nic_name ... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

8. Shell Programming and Scripting

perl hash of hashes from database

hi there, I have some database output that looks like this SELECT nic_name,nic_duplex,nic_speed,nic_ip FROM network_table WHERE hostname = "server1" result is this (ive delimited with a pipe for ease of reading) bge0|full|1000|10.32.100.1 bge1|full|1000|11.12.101.7 ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

9. Shell Programming and Scripting

Print Entire hash list (hash of hashes)

I have a script with dynamic hash of hashes , and I want to print the entire hash (with all other hashes). Itried to do it recursively by checking if the current key is a hash and if yes call the current function again with refference to the sub hash. Most of the printing seems to be OK but in... (1 Reply)
Discussion started by: Alalush
1 Replies

10. Shell Programming and Scripting

Perl hashes "Can't use subscript on private hash"

This is driving me mad, where am I going wrong? The relevant segment of code: sub getndsybcons { my @servers=@{$_}; my @sybservers=@{$_}; my %results; foreach my $server(@servers) { my $biggestsyb; my $biggestsybval=0; ... (9 Replies)
Discussion started by: Smiling Dragon
9 Replies
Login or Register to Ask a Question