Tie a large list of DB_Files

Discussion in 'Perl' started by Antonio Gulli, Dec 27, 2003.

  1. 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.
     
    Antonio Gulli, Dec 27, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan Weekes

    Tie to change line in file

    Jonathan Weekes, Sep 14, 2003, in forum: Perl
    Replies:
    0
    Views:
    503
    Jonathan Weekes
    Sep 14, 2003
  2. Carlo Filippini

    Tie a file handle to a variable

    Carlo Filippini, Sep 29, 2003, in forum: Perl
    Replies:
    1
    Views:
    454
  3. Carlo Filippini

    Tie a file handle to a variable

    Carlo Filippini, Sep 29, 2003, in forum: Perl
    Replies:
    3
    Views:
    643
    Kris Wempa
    Oct 1, 2003
  4. SpecialK

    Tie a large file to a Hash

    SpecialK, Feb 19, 2004, in forum: Perl
    Replies:
    0
    Views:
    467
    SpecialK
    Feb 19, 2004
  5. botfood

    tie() with DB_File not tie()ing ?

    botfood, Apr 24, 2006, in forum: Perl Misc
    Replies:
    23
    Views:
    481
    botfood
    Apr 26, 2006
Loading...

Share This Page