Replies: 1 comment
-
This works for me use strict ;
use warnings;
use BerkeleyDB ;
my $filename = "tree" ;
unlink $filename;
my $db = new BerkeleyDB::Btree
-Filename => $filename,
-Flags => DB_CREATE,
-Compare => sub { lc $_[0] <=> lc $_[1] }
or die "Cannot open $filename: $!\n" ;
print "Adding records\n";
# Add a key/value pair to the file
$db->db_put(1, 'Larry') ;
$db->db_put(3, 'John') ;
$db->db_put(11, 'mickey') ;
$db->db_put(33, 'donald') ;
$db->db_put(9, 'joe') ;
print "contents of the file\n" ;
my ($k, $v) = ("", "") ;
my $cursor1 = $db->db_cursor() ;
while ($cursor1->c_get($k, $v, DB_NEXT) == 0)
{
print "$k -> $v\n"
}
print "\n";
my $cursor2 = $db->db_cursor();
$cursor2->c_get($k, $v, DB_FIRST);
print "FIRST $k -> $v\n";
$cursor2->c_get($k, $v, DB_LAST);
print "LAST $k -> $v\n"; when I run it, I get this
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I'm having an issue with sorting duplicate keys for a Btree database. From the documentation:
Will this work with the new declaration, i.e.:
Notice that I've altered the sorting to a numerical sort instead of a lexical sort. My keys are the numbers 1, 2,.., 10. I have tried this, and then created a cursor object a la:
Which gives me the first and last record according to a lexical sort, not the numerical sorting I'm after. In other words, I get the record for 9 instead of 10 which is consistent with a lexical sort. Is there a way to get the numerical sorting using my Btree definition and using cursors? if not, can I accomplish this without using a cursor? Or is there some other way to do this?
Thank you for your consideration,
Marc
Beta Was this translation helpful? Give feedback.
All reactions