Reputation: 11
I have a hash %AllData
pulling data from database using following code:
while(my $Row = $sth1->fetchrow_hashref())
{
if(defined($Row->{LotID}))
{
$AllData->{$Row->{LotID}}->{$Row->{Name}} = $Row->{Details};
}
}
After I'm done with pulling data, I use print Dumper($AllData);
to print out All my data which show like:
$VAR1 = {
'4197359' => {
'Short Description' => 'Rock Picker',
'Lot Number' => '1065',
'Description' => 'Rock Picker'
},
'4194148' => {
'Short Description' => 'Large work bench w/ drawers',
'Lot Number' => '1041',
'Description' => 'Large work bench w/ drawers'
},
'4200944' => {
'Lot Number' => '1084',
'Description' => 'Horse Trailer'
}
}
However, when I try to print out the size of the hash or use foreach
to access the hash, it shows 0 size and can't access any element within the hash:
print "Hash Size: ", scalar keys %AllData, "\n";
shows:
Hash Size: 0
What's the cause of my problem?
Upvotes: 1
Views: 373
Reputation: 13163
should represent a hash ref as $%hash
instead of %hash
to print
Upvotes: 0
Reputation: 3436
Try accessing scalar keys %$AllData
in order to access the hash that the reference.. refers to.
$AllData
(what you're passing to Dumper()
) is a reference to a hash (a 'hashref')
%AllData
is a different thing to Perl than $AllData
. If this hasn't been set yet and perl isn't complaining, you may need to try putting use strict;
at the top of your script so that you can be warned of these (and other) types of errors.
Upvotes: 4
Reputation: 126742
There is no hash %AllData
, and if your program didn't raise an error then you haven't got
use strict;
use warnings;
at the head of you program. This is vital for all Perl programs, especially when you are asking others for help with your code.
The hash you're interested in is the one referenced by $AllData
, so you need to use this variable and dereference it. Like this
print "Hash Size: ", scalar keys %$AllData, "\n";
Upvotes: 6
Reputation: 62154
Maybe you need to dereference the hash first:
print "Hash Size: ", scalar keys %{ $AllData }, "\n";
Upvotes: 2