Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: Database storage confusion

by Coruscate (Sexton)
on Mar 09, 2003 at 00:37 UTC ( [id://241459]=note: print w/replies, xml ) Need Help??


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

Yes, that's exactly what merlyn is trying to get across. A database is pretty useless if you are only storing a single value. Much easier to simply manage a flat file and be able to read it into an array. A database becomes useful when you need to associate one piece of data with another.

For your example, you are not associating anything with the name. All you have is a name. No age, email adress, phone number, or anything related to the user. If you did have anything, you'd then be using something like this:

# I've never really used a dbm. # They allow complicated structures, right? $dbm{$name}{'age'} = 29; $dbm{$name}{'email'} = 'nobody@example.com';

For a single value, I'd probably use something like this. It could use a little more work :)

use Fcntl ':flock'; sub add_names { my @add = @_; open my $fh, '+<', 'names.dat'; flock $fh, LOCK_EX; chomp( my @names = <$fh> ); seek $fh, 0, 2; for my $name (@add) { warn "$name is already in file" and next if grep { /^$name$/i } @names; print $fh "$name\n"; } close $fh; } sub get_names { open my $fh, '<', 'names.dat'; chomp( my @names = <$fh> ); close $fh; return @names; } sub del_names { my @del = @_; open my $fh, '+<', 'names.dat'; flock $fh, LOCK_EX; chomp( my @names = <$fh> ); truncate $fh, 0; seek $fh, 0, 0; for my $name (@names) { print $fh "$name\n" unless grep { /^$name$/i } @del; } close $fh; }


If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

Replies are listed 'Best First'.
Re4: Database storage confusion
by blakem (Monsignor) on Mar 09, 2003 at 05:57 UTC
    # I've never really used a dbm.
    # They allow complicated structures, right?
    $dbm{$name}{'age'} = 29;
    Well, some do, and some dont... MLDBM certainly does, though your syntax wont work. Quoting from the pod:
    Adding or altering substructures to a hash value is not entirely transparent in current perl. If you want o store a reference or modify an existing reference value in the DBM, it must first be retrieved and stored in a temporary variable for further modifications. In particular, something like this will NOT work properly:
    $mldb{key}{subkey}[3] = 'stuff'; # won't work
    Instead, that must be written as:
    $tmp = $mldb{key}; # retrieve value $tmp->{subkey}[3] = 'stuff'; $mldb{key} = $tmp; # store value

    This limitation exists because the perl TIEHASH interface currently has no support for multidimensional ties.

    -Blake

Re: Re^3: Database storage confusion
by sulfericacid (Deacon) on Mar 09, 2003 at 01:08 UTC
    The reason I didn't want to go back to flat files is because of the nagging need to flock and seek all the time. Databases are so much easier to open, use and store than a flatfile is.

    You're a major bummer for writing out the script I was trying to make, now what's to stop my evil side from just using that and say I made it? lol.

    I've never seen anything like $dbm{$name}{'age'} = 29; before, I'm going to have to check that out.

    Thanks!!!

    "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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://241459]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found