in reply to Re: Database storage confusion
in thread Database storage confusion

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

Replies are listed 'Best First'.
Re: Re: Re: Database storage confusion
by demerphq (Chancellor) on Mar 09, 2003 at 15:38 UTC
    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

        MLDBM does something like a more sophisticated

        # Store $dbm{$name}=join "\0",$number,$address,$citty,$emailaddress; # Fetch my ($number,$address,$citty,$emailaddress)=split /\0/,$dbm{$name};

        Which would perfectly suitable for your limited requirements. (Well, it assumes that there are no \0 in the input data. A safe bet in this situation I think, and easily enforced) But certainly do look into using the module, although for something this simple I wouldnt bother with MLDBM at all. Now if you want to store a sophisticated data structure like a tree or something, then MLDBM gets pulled off the shelf.


        ---
        demerphq


Re: Re: Re: Database storage confusion
by Cody Pendant (Prior) on Mar 09, 2003 at 06:38 UTC
    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