开发者

How do I see how many keys are in a Berkeley DB (Perl)?

I simply need to find out how many elements I have in this hash, and I'm looking at the docs but the only开发者_如何学JAVA thing that might give me this is db_stat, and there's a TODO under it.

http://search.cpan.org/dist/BerkeleyDB/BerkeleyDB.pod#Using_db_stat

So other than creating a cursor and parsing through the entire thing, how can I see how many keys are in the hash?


Seem to have found the answer here:

http://download.oracle.com/docs/cd/E17076_02/html/api_reference/C/dbstat.html

Names many different variables set when db_stat is called, including the one I was looking for, 'hash_ndata', which gives the number of key-value pairs.


You can tie a bdb database to a hash, then check the number of keys in the hash. The keys function will give you the number of keys in a hash when used in a scalar context.

use DB_File;
our %dbHash;

yadda, yadda, yadda;
tie (%dbHash, "DB_File", $dbFileName);
print "The number of keys in $dbFileName is " . keys(%dbHash) . "\n";

Unfortunately, I can't test this because I don't have dbd on my system. I believe you have to use package variables for handling the tie.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜