traveler has asked for the wisdom of the Perl Monks concerning the following question:

Wise ones:
I tie a hash to an existing file with: tie (%hash, DB_File', "myfile") or die "Can't open $filename: $!\n"; I'd now like to delete the entry (and key!) whose key is, say, "foo". I read in What's the difference between "delete" and "undef" with hashes? about delete and undef, but it says that tied databases may not do delete and undef as normal hashes do.
Any suggestions?

--traveler

Replies are listed 'Best First'.
Re: How to delete a GDBM entry?
by extremely (Priest) on Mar 16, 2001 at 02:53 UTC
    Do the delete and try it. Under GDBM it should work. And reread that article again, it doesn't say what you seem to think it says.

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: How to delete a GDBM entry?
by lachoy (Parson) on Mar 16, 2001 at 03:22 UTC

    delete definitely works, but you probably made a typo in the code: you need to specify the right module, and make sure you're using the right permissions:

    tie (%hash, 'GDBM_File', "myfile", &GDBM_WRITER ) or die "Can't open $filename: $!\n";

    (Depending on how the exporter is working, you might need to use &GDBM::GDBM_WRITER.)

    Your GDBM might default to GDBM_READER, which won't let you delete or modify anything.

    Chris
    M-x auto-bs-mode

Re: How to delete a GDBM entry?
by tomhukins (Curate) on Mar 16, 2001 at 17:08 UTC

    Although you can use delete to remove an entry from your %hash, this won't free up any space in your GDBM file automatically.

    Look for gdbm_reorganize in GDBM's documentation. If you don't call this function, your file will continually grow.

    For more details, look for reorganize on this page.