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

Can anyone tell me, if I add a record to a hash db that has 10 fields, with one field being an order number. I increment the order number, but I keep blowing away/overwriting the information in the other keys of the hash. How do I keep from doing this?
sub dborder { dbmopen(%order, "order", 0644) || die "Unable to open db : $!"; $order{'o'} = $order; $order{"name"} = $name; $order{"address"} = $add; $order{"city"} = $city; $order{"state"} = $state; $order{"zip"} = $zip; $order{"country"} = $country; $order{"email"} = $email; $order{"cost"} = $cost; $order{'a'} = [@food]; $order{'b'} = [@fries]; $order{'c'} = [@onionrings]; $order{'d'} = [@drink]; $order{'o'}++; dbmclose(%order) || die "Cannot close dbm file.\n"; }
curtisb

Replies are listed 'Best First'.
(tye)Re: Adding records to a hash db
by tye (Sage) on Oct 03, 2000 at 01:47 UTC

    You are using the same keys to the same database each time so, of course, it blows away the previous values.

    You could incorporate the order number into each key:

    sub dborder { dbmopen(%order, "order", 0644) || die "Unable to open db : $!"; $order{'o'} = $order; $order{"name$order"} = $name; $order{"address$order"} = $add; $order{"city$order"} = $city; $order{"state$order"} = $state; $order{"zip$order"} = $zip; $order{"country$order"} = $country; $order{"email$order"} = $email; $order{"cost$order"} = $cost; $order{'a'.$order} = [@food]; $order{'b'.$order} = [@fries]; $order{'c'.$order} = [@onionrings]; $order{'d'.$order} = [@drink]; $order{'o'}++; dbmclose(%order) || die "Cannot close dbm file.\n"; }

    Or you could just store all of this under a single key that is the order number if you use something like MLDBM (?) to serialize your Perl data structures. Since you aren't already using MLDBM, I don't think your [@food] will actually store anything useful in the database. But then I don't know much about this kind of thing (and wasn't going to reply) so I hope this is of some help.

            - tye (but my friends call me "Tye")
Re: Adding records to a hash db
by merlyn (Sage) on Oct 03, 2000 at 04:09 UTC
(jeffa) Re: Adding records to a hash db
by jeffa (Bishop) on Oct 03, 2000 at 01:24 UTC
    Could you just change the first assignment to:
    $order{'o'} = $order + 1;
    Jeff

    UPDATE:
    That was pretty stupid, what the heck was I thinking? Thanks for the 'heads-up' Fastolfe.

      This seems suspicious. Are we sure that this statement is what is "blowing away" the record?
      Thanks for the tip. I will try that.