Hello all,
I am working on a script that reads a config file and loads it into a HoH as below:
The I run a Query in a subroutine that creates another HoH and returns it as below:
All well and good but I am having trouble using the returned reference to the HoH returned from the subroutine. What I want to do is loop through each entry in the HoH returned from the sub and do a lookup in the config file HoH to determine what commands to run on the device based on the $vendor and $device in the config file HOH. Here is what I have tried (messy)
This does not work as the output of the print is:
I then attempt to loop through the HoH from the query as so:
Which is not working either. Maybe I am approaching this wrong as this is my first real foray into HoHs and I'm not sure they are made to be looped through.
Thanks,
Nick
If at first you don't succeed, don't try skydiving.
I am working on a script that reads a config file and loads it into a HoH as below:
Code:
open (CFG, "$cfg_file") or die ("Error opening config file $cfg_file: $1");
#
# Read config file, load a Vendor => Device Type => CLI hash of hashes. Count the entries.
#
my %cmd;
my ($vendor, $device, $rest);
while (<CFG>) {
next if /^#/;
chomp;
($vendor, $device, $rest) = split(/\s*\&\s*/);
if ($vendor) {
$cmd{$vendor}->{$device} = $rest;
my $hoh_cnt = scalar(keys(%cmd));
}
}
close CFG;
The I run a Query in a subroutine that creates another HoH and returns it as below:
Code:
sub VistaQuery
{
##########################################################################################
#
# Query the InfoVista DB and build an ip to vendor hash for all the devices
# we will be querying.
#
##########################################################################################
my $sub_vendor;
my $sub_ip;
my $sub_dev_type;
my %sub_db_results;
my $data_source = "dbi:Oracle:binvdp01";
my $dbh = DBI->connect($data_source, $orauser, $orapass)
or die "Can't connect to $data_source: $DBI::errstr";
my $sth = $dbh->prepare( q{select T1.SVALUE, T2.SVALUE, T3.SVALUE from twtpropvalues T1, twtpropvalues T2, twtpropvalues T3 where T1.PROPID=(select ID from twtproperties where WID='00000000000000000000000000000001') and T2.PROPID=(select ID from twtproperties where WID='72B1577C795AB24888F4462B18D8F42D') and T3.PROPID=(select ID from twtproperties where WID='069003638BAFDA11BCB000166F0D3E5D') and T1.INSID=T2.INSID and T2.INSID=T3.INSID
}
) or die "Can't prepare statement: $DBI::errstr";
my $rc = $sth->execute
or die "Can't execute statement: $DBI::errstr";
print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n";
print "Field names: @{ $sth->{NAME} }\n";
while (($sub_ip, $sub_vendor, $sub_dev_type) = $sth->fetchrow_array) {
$sub_db_results{$sub_vendor}->{$sub_dev_type} = $sub_ip;
}
#
# search for problems which may have terminated the fetch early, disconnect and return the hash.
#
die $sth->errstr if $sth->err;
$dbh->disconnect;
return \%sub_db_results;
}# End sub VistaQuery
All well and good but I am having trouble using the returned reference to the HoH returned from the subroutine. What I want to do is loop through each entry in the HoH returned from the sub and do a lookup in the config file HoH to determine what commands to run on the device based on the $vendor and $device in the config file HOH. Here is what I have tried (messy)
Code:
my %db_results;
my $db_results = VistaQuery();
my ($ip,$cli1);
for my $key ( keys %$db_results ) {
$cli1 = $db_results->{ $key }->{'$device'};
print "ip: $ip, vendor: $vendor, cli: $cli1\n";
}
This does not work as the output of the print is:
Code:
ip: , vendor: Cisco, cli:
ip: , vendor: Cisco, cli:
ip: , vendor: Cisco, cli:
I then attempt to loop through the HoH from the query as so:
Code:
while(my($ip,$device,$vendor) = each %db_results) {
my @time_stamp;
next unless ($vendor);
print "$ip => $vendor\n";
my $cli = $cmd{'$vendor'}->{'$device'};
print "$cli\n";
etc. etc.
Which is not working either. Maybe I am approaching this wrong as this is my first real foray into HoHs and I'm not sure they are made to be looped through.
Thanks,
Nick
If at first you don't succeed, don't try skydiving.