Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

(podmaster) Re: In order traversal of BTREE keys where not all keys have duplicate values (using DB_File)

by PodMaster (Abbot)
on May 19, 2002 at 23:52 UTC ( [id://167741]=note: print w/replies, xml ) Need Help??


in reply to In order traversal of BTREE keys where not all keys have duplicate values (using DB_File)

What is the definition of your sub _compare?
(incomplete code cannot possibly demonstrate the problem)

Use the find_dup method to see if there are any duplicates.
(change the format of your values to help you out here, or just forget the whole R_DUP thing, and do something with the keys like some_key_part_1, some_key_part_2 ...)

Here is an example adapted from the DB_File documentation to show you a neat little trick

use strict ; use DB_File ; use vars qw( $x %h ) ; # Enable duplicate records $DB_BTREE->{'flags'} = R_DUP ; $x = tie %h, "DB_File", undef, O_RDWR|O_CREAT, 0640, $DB_BTREE or die "Cannot open $filename: $!\n"; $h{'Wall'} = 'only once'; $h{'Smith'} = 'more than once'; $h{'Smith'} = 'more than twice'; $h{'Smith'} = 'three times'; my $cnt = $x->get_dup("Wall") ; print "Wall occurred $cnt times\n" ; my %hash = $x->get_dup("Wall", 1) ; print "Larry is there\n" if $hash{'Larry'} ; print "There are $hash{'Brick'} Brick Walls\n" ; my @list = sort $x->get_dup("Wall") ; print "Wall => [@list]\n" ; @list = $x->get_dup("Smith") ; print "Smith => [@list]\n" ; @list = $x->get_dup("Dog") ; print "Dog => [@list]\n" ;
particularly interesting is this tidbit
$count = $x->get_dup($key) ; @list = $x->get_dup($key) ; %list = $x->get_dup($key, 1) ;

 

Look ma', I'm on CPAN.


** The Third rule of perl club is a statement of fact: pod is sexy.
  • Comment on (podmaster) Re: In order traversal of BTREE keys where not all keys have duplicate values (using DB_File)
  • Select or Download Code

Replies are listed 'Best First'.
Re: (podmaster) Re: In order traversal of BTREE keys where not all keys have duplicate values (using DB_File)
by stajich (Chaplain) on May 20, 2002 at 15:19 UTC
    Oops missing the compare function.
     sub _compare { $_[0] <=> $_[1] }

    Keys are numeric bins of the form 10000.000178 - this adapted from a mysql strategy where we are testing for bins within the boundaries but I am interested in an in-memory adaptation.

    I want to iterate through a subset of the keys that are bounded by some values $begin, $end not the whole set of keys as is demonstrated in all the examples. I was using get_dup to initialize the cursor and then iterate through all the available keys, however the cursor will only point to keys which have >1 value.
    ===
    Ahem. I was making it harder than it should be.

    $bhandle->seq($start,$v,R_CURSOR); while( $bhandle->seq($k,$v,R_NEXT) == 0 ) { last if( $k> $end); ... }
    Gets me what I want. Calling get_dup to initialize the cursor was not a smart idea and was the root of the problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://167741]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-25 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found