in reply to Database storage confusion

$dbm{'name'} = $name overwrites the previes entry $dbm{$name'} = $name creates redundant Aaron Aaron $dbm{$name}; creates Useless use of DBM error $dbm{$name} = ""; writes Aaron
I don't understand a couple of those:
$dbm{'name'} = $name overwrites the previes entry # Yes, quite correct $dbm{$name'} = $name creates redundant Aaron Aaron # I don't think so. You've got bad punctuation there, what do you expect Perl to do? Perhaps you're creating a value of "Aaron" but with a key of "Aaron'" -- see quotes? $dbm{$name}; creates Useless use of DBM error # quite so. Meaningless if that's the whole statement. $dbm{$name} = ""; writes Aaron # no it doesn't, I'm betting. $dbm{$name} should be an empty string now.

Prove it to us with some more code?

Like for instance:

$name = Aaron; $dbm{$name} = $name; print $dbm{$name}; $dbm{$name'} = $name; print $dbm{$name'};
and so on. --
“Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
M-J D

Replies are listed 'Best First'.
Re: Re: Database storage confusion
by sulfericacid (Deacon) on Mar 09, 2003 at 00:44 UTC
    I will post my code at the bottom of this message. What I meant with $dbm{$name} = $name; was it would hold Aaron as the key and Aaron as the value so if I were to print out the database it would print Aaron Aaron.

    The code that works for me is actually $dbm{$name} = "";, it's the only one that does what I want. You can test the script itself from what I posted below.

    #!/usr/bin/perl -W use strict; use warnings; use POSIX; use CGI; require SDBM_File; my %dbm; my $test = "test.dbm"; tie (%dbm, 'SDBM_File', $test, O_CREAT|O_RDWR, 0644) || die "Died tying database\nReason: $!\n"; print "Aaron's Phonebook System\n\n\n"; print "What is their name?\n"; my $name = $_; #if ($name ne "") { chomp($name = <STDIN>); $dbm{$name} = ""; print "Added to the list!\n";


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid
      print "What is their name?\n"; my $name = $_; #if ($name ne "") { chomp($name = <STDIN>); $dbm{$name} = ""; print "Added to the list!\n";

      should read more like

      print "What is their name?\n"; chomp($name = <STDIN>); chomp($number = <STDIN>); $dbm{$name} = $number; print "$name = $dbm{$name} added to the list!\n";

      shouldn't it?


      ---
      demerphq


        I suppose but I find it easier to write $_ = <STDIN> then assign a variable to hold $_, makes it easier for me to understand or see I guess.

        I already knew how to assign a value to the database key such as your phone number, the thing I'm trying to do is assign more than one value. Ie. I want $number, $address, $city, $emailaddress all stored under $name and have each of them retrievable. I'm reading up on MLDBM since some people yesterday said that would do the trick.

        Thanks for your help!

        "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

        sulfericacid
      Honestly, I'm more confused than ever now that you've posted that code.
      --
      “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
      M-J D