Visit Our UNIX and Linux User Community


PERL - another quick hash of hashes question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL - another quick hash of hashes question
# 1  
Old 08-30-2009
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

Code:
   serial     nic_name      nic_ip   nic_duplex      nic_speed

0629AN1200 	bge0 	    1.1.1.1 	full     	10 
0629AN1200 	bge1 	    8.8.8.8 	full     	100 
0629AN1200 	bge2 	    7.7.7.7 	half    	100 	
0629AN1200 	bge3        3.2.1.1 	full 	        1000

i am creating a hash of hashes from this data ,using the code below

Code:
#!/usr/bin/perl -w
use DBI;

my $serial = "0629AN1200";   # an example serial number

my $dbh = DBI->connect("DBI:mysql:CMDB","username","password",) or die $DBI::errstr;
my $sth = $dbh->prepare("SELECT nic_name,nic_ip,nic_duplex,nic_speed FROM view1 WHERE serial = '$serial'") or die $DBI::errstr;
$sth->execute or die $DBI::errstr;
$hashr = $sth->fetchall_hashref('nic_name');

# I am using this bit of code to display the contents and structure of the hash of hashes

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


when i run this script I get this output...note that even though I have asked fetchall_hashref to create keys based on 'nic_name', the output below still ALSO has 'nic_name' as a key value pair of the sub hash ..... ?


Code:
$ ./tester2.pl
bge3 ->
        nic_ip -> 3.2.1.1
        nic_speed -> 1000
        nic_name -> bge3
        nic_duplex -> full
bge1 ->
        nic_ip -> 8.8.8.8
        nic_speed -> 100
        nic_name -> bge1
        nic_duplex -> full
bge0 ->
        nic_ip -> 1.1.1.1
        nic_speed -> 10
        nic_name -> bge0
        nic_duplex -> full
bge2 ->
        nic_ip -> 7.7.7.7
        nic_speed -> 100
        nic_name -> bge2
        nic_duplex -> half

Is there something im doing wrong here ? I was under the impression that the line ...

Code:
$hashr = $sth->fetchall_hashref('nic_name');

would set the top level keys of the hash of hashes. .(which it seems to have successfuilly done) ... but would then have the intelligence to omit that particular key/value pair from the sub hash?

am i missing something here??




Any help would be great

Last edited by hcclnoodles; 08-30-2009 at 05:46 PM..
# 2  
Old 08-30-2009
Quote:
Originally Posted by hcclnoodles
...
...
Is there something im doing wrong here ? I was under the impression that the line ...

Code:
$hashr = $sth->fetchall_hashref('nic_name');

would set the top level keys of the hash of hashes. .(which it seems to have successfuilly done) ... but would then have the intelligence to omit that particular key/value pair from the sub hash?
...
That is by design. Check the DBI documentation at cpan.org.

tyler_durden
# 3  
Old 08-31-2009
thanks, its a bit of a pain considering im looping through all the key/value pairs in the sub hash. im going to have to code in an exception as technically its repeated information ...

thanks for clearing it up

Previous Thread | Next Thread
Test Your Knowledge in Computers #197
Difficulty: Easy
C# ranked higher than C according to the TIOBE Index for October 2019.
True or False?

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

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

3. 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

4. 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

5. Shell Programming and Scripting

Perl: Any quick way to use regex on hash keys?

Hi, Is there any quick way to use pull out keys that match a specific regex pattern? eg %hash ; $hash(123,456) = xxx; $hash(123,457) = xxx; $hash(123,458) = xxx; $hash(223,459) = xxx; I need a fast way to get all the keys that start with 123.. Meaning I should get ... (5 Replies)
Discussion started by: Leion
5 Replies

6. Shell Programming and Scripting

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 ... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

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

8. 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

9. 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

10. Shell Programming and Scripting

perl hashes question

hi guys im running into a problem here this is my script #!/usr/bin/perl use CGI qw(:standard); $header = "MIME-Version: 1.0\n"; $header .= "Content-type: text/html\n"; $header .= "\n"; #get the point parameter from nhl.html $Team = param("points"); print "$header"; open(INFILE,... (1 Reply)
Discussion started by: lucho_1
1 Replies

Featured Tech Videos