use strict; use DB_File; my %btree; $DB_BTREE->{'flags'} = R_DUP; my $bhandle = tie %btree, 'DB_File', undef, O_RDWR|O_CREAT, 0666, $DB_BTREE; my @array = ( 'a'..'z' ); foreach ( '2' .. '6') {$btree{$_} = shift @array;} @array = ( 'A'..'Z' ); foreach ( '2' .. '6' ) {$btree{$_} = shift @array;} $btree{1}='HHI'; $btree{4.5}='HHI'; $btree{7}='HHI'; $btree{8}='HHI'; print "From each:\n"; while (my ($key,$val)=each %btree) { print "'$key' contains ".$bhandle->get_dup($key)." values\n"; } print "\n"; print "From for:\n"; my ($key,$val); for (my $status = $bhandle->seq($key, $val, R_FIRST()) ; $status == 0 ; $status = $bhandle->seq($key, $val, R_NEXT()) ) { print "'$key' contains ".$bhandle->get_dup($key)." values\n"; } #### sub get_dup { croak "Usage: \$db->get_dup(key [,flag])\n" unless @_ == 2 or @_ == 3 ; my $db = shift ; my $key = shift ; my $flag = shift ; my $value = 0 ; my $origkey = $key ; my $wantarray = wantarray ; my %values = () ; my @values = () ; my $counter = 0 ; my $status = 0 ; # iterate through the database until either EOF ($status == 0) # or a different key is encountered ($key ne $origkey). for ($status = $db->seq($key, $value, R_CURSOR()) ; $status == 0 and $key eq $origkey ; $status = $db->seq($key, $value, R_NEXT()) ) { # save the value or count number of matches if ($wantarray) { if ($flag) { ++ $values{$value} } else { push (@values, $value) } } else { ++ $counter } } return ($wantarray ? ($flag ? %values : @values) : $counter) ; }