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 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
#!/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 ..... ?
$ ./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 ...
$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