Reputation: 113
i am wondering if someone can help me with a frustrating problem with an old perl code. My actual task is to migrate an old perl script from Version 5.10.1 to 5.18.2.
So i've done already the simple problems with the script like wrong calls, bad format, ect.
But im stuck on a function called fetchhash with seem to be like fetchrow_hashref;
Im intended to catch a list of IPs, Nameserver IDs and Names. My Problem is, that the function fetchrow_hashref doesn't do the same thing like fetchhash in the old code.
see folllowing new code:
#fetch_network();
$res2=$dbh->prepare("select nsip,nsid,nsname from nameservers");
$res2->execute();
while (%ns2=$res2->fetchrow_hashref;{
$nsips{$ns2{nsid}}=$ns2{nsip};
$nsnames{$ns2{nsid}}=$ns2{nsname};
}
The table looks like:
+-----------------+------+------------------------------+
| nsip | nsid | nsname |
+-----------------+------+------------------------------+
| 101.101.111.10 | 1 | ns1.yournameserver.net |
Here is a part of the old code:
$res2=$dbh->query("select nsip,nsid,nsname from nameservers");
while (%ns2=$res2->fetchhash)
{
$nsips{$ns2{nsid}}=$ns2{nsip};
$nsnames{$ns2{nsid}}=$ns2{nsname};
}
So i tried with fetchrow_hashref but it did'nt work for me:
main::(nsconfgen.pl:170): while (%ns2=$res2->fetchrow_hashref){
DB<193> n
Reference found where even-sized list expected at nsconfgen.pl line 170.
at nsconfgen.pl line 170.
main::(nsconfgen.pl:171): $nsips{$ns2{nsid}}=$ns2{nsip};
DB<193> p $ns2{nsid}
( i've get no value from the hash )
Of course i've crawled through Google and i tried a lot of examples but nothing helps realy...
So i hope i can get here some help.
Thank you very much!
Upvotes: 0
Views: 300
Reputation: 118595
In DBI
implementors, the return value of fetchrow_hashref
is a reference to a hash, not a hash. A few changes to your code will get it right:
while ($ns2=$res2->fetchrow_hashref) {
$nsips{$ns2->{nsid}}=$ns2->{nsip};
$nsnames{$ns2->{nsid}}=$ns2->{nsname};
}
Upvotes: 1