Next | Lightweight Databases | 96 |
Unlike a hash, a B-tree may store more than one value per key
To enable this, use R_DUP:
my $dup_btree = DB_File::BTREEINFO->new(); $dup_btree->{flags} = R_DUP; tie %hash, 'DB_File', $file, O_CREAT|O_RDWR, 0666, $dup_btree;
while (<FRUITS>) { my ($color, $fruit) = split / /, $_, 2; $hash{$color} = $fruit; }
Ordinary hash assignment actually stores the new value in addition to the old one
Hash retrieval recovers only the first stored value
But ->seq will recover all the values:
$k = "red"; for ($fail = $db->seq($k, $v, R_CURSOR); ! $fail && $k eq "red"; $fail = $db->seq($k, $v, R_NEXT)) { print "$k: $v\n"; }
red: apple red: cherry red: strawberry red: raspberry
Next | Copyright © 2003 M. J. Dominus |