On 07/08/2012 11:15, punit jain wrote:
I am using DBM::Deep for the reason it support multilevel hashes, however I
ended up in situation where I am getting error while retrieving values from
keys :-
Can't use string ("test1") as a HASH ref while "strict refs"
use Parallel::ForkManager;
use DBM::Deep;
my $db = new DBM::Deep(
file => "userdata.db",
locking => 1,
autoflush => 1
);
passed this ref to a subroutine...
where I input the values as :-
$entry->{'name'} = $name;
$entry->{'id'} = $id;
$users->{'pending'}->{$entry->{'name'}}->{'email'}=$name;
$users->{'pending'}->{$entry->{'name'}}->{'id'}=$id;
return $users;
and in other routine I get values :
foreach my $acct (keys %{$users->{pending}}) {
print $acct->{id};
}
However I get error saying :-
Can't use string ("user1") as a HASH ref while "strict refs" in use
As you have specified, the values of $acct are the *keys* of the hash at
$users->{pending}. Keys are simple strings and can't be dereferenced.
What you need to do is write
foreach my $acct (keys %{$users->{pending}}) {
print $users->{pending}{$acct}{id};
}
or you can loop over the *values* instead and write
foreach my $data (values %{$users->{pending}}) {
print $data->{id};
}
but this way you have no access to the name of the account you are
processing. For the best of both worlds you can write
while ( my ($acct, $data) = each %{ $users->{pending} } ) {
print "$acct\n";
print $data->{id}, "\n";
}
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/