A
Antonio Gulli
I have a large list of DB_Files stored in a directory. I would like to
tie all of them to an array of hashes and to search them for key,
using the hashes.
My idea is rather simple:
my (@listOfHashes);
my ($numOfHashes);
sub loadDBs{
my ($dbDIR) = @_;
my ($numFiles) = 0;
my ($b) = new DB_File::BTREEINFO;
my ($file);
$numOfHashes = 0;
opendir(DIR, $dbDIR) || die "cannot opendir $dbDIR: $!";
while (defined($file = readdir(DIR))){
if ($file =~ /db\.*/){
tie %{ $listOfHashes[$numOfHashes] }, "DB_File", $file,
O_RDONLY, 0666, $b
|| die ("Cannot open $file");
$numOfHashes++;
print "Tied $file $numOfHashes\n";
}
}
}
sub dumpAll_Key_Value{
my ($key, $value);
for (my $i = 0; $i < $numOfHashes; $i++){
print "Load $i";
while (($key, $value) = keys %{ $listOfHashes[$i] }){
print ">$key< >$value<\n";
}
}
}
sub searchKey{
my ($key) = @_;
my ($fetched);
for (my $i = 0; $i < $numOfHashes; $i++){
if (defined ($fetched = $listOfHashes[$i]{$key})){
print "found $key on $i DB";
return $fetched;
}
}
return undef;
}
But neither searchKey() nor dumAll_Key_Value() seems to work,
while loadDBs() seems to tie the hashes...
Am i doing a trivial error or what? Any suggestion?
Thank you so much.
tie all of them to an array of hashes and to search them for key,
using the hashes.
My idea is rather simple:
my (@listOfHashes);
my ($numOfHashes);
sub loadDBs{
my ($dbDIR) = @_;
my ($numFiles) = 0;
my ($b) = new DB_File::BTREEINFO;
my ($file);
$numOfHashes = 0;
opendir(DIR, $dbDIR) || die "cannot opendir $dbDIR: $!";
while (defined($file = readdir(DIR))){
if ($file =~ /db\.*/){
tie %{ $listOfHashes[$numOfHashes] }, "DB_File", $file,
O_RDONLY, 0666, $b
|| die ("Cannot open $file");
$numOfHashes++;
print "Tied $file $numOfHashes\n";
}
}
}
sub dumpAll_Key_Value{
my ($key, $value);
for (my $i = 0; $i < $numOfHashes; $i++){
print "Load $i";
while (($key, $value) = keys %{ $listOfHashes[$i] }){
print ">$key< >$value<\n";
}
}
}
sub searchKey{
my ($key) = @_;
my ($fetched);
for (my $i = 0; $i < $numOfHashes; $i++){
if (defined ($fetched = $listOfHashes[$i]{$key})){
print "found $key on $i DB";
return $fetched;
}
}
return undef;
}
But neither searchKey() nor dumAll_Key_Value() seems to work,
while loadDBs() seems to tie the hashes...
Am i doing a trivial error or what? Any suggestion?
Thank you so much.