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 | |
|
Re: Re^3: Database storage confusion
by sulfericacid (Deacon) on Mar 09, 2003 at 01:08 UTC |