Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: How to modify/delete records in a flat-file database?

by TwistedGreen (Sexton)
on Apr 28, 2004 at 12:34 UTC ( #348799=note: print w/replies, xml ) Need Help??

in reply to How to modify/delete records in a flat-file database?

If you still need to use a CSV (which may be useful if it needs to be human-readable, for example) there is another method. Instead of thinking of changing one row in a file, you should think of it as changing the entire file. This may be a bit backwards compared to some of those fancy CPAN modules, but it's simple and it works.

Basically, what you'd want to do is read the entire file into a series of hashes, using the username as the key. Then you would make whatever change needs to be made to the hash in memory, and write the entire file back to the disk. This would have the effect of changing one line but would really be overwriting the entire file.

I recently wrote a CGI script that will allow changes to be made to a CSV remotely, so I'll borrow code from it to illustrate this solution, based on your given data set.
my @names; my %nums; my %stats; my %oses; my $i=0; my $csv="data.csv"; # read data from $csv open CSV, $csv or die; flock(CSV, 1); while (<CSV>) { chomp; ($name,$num,$stat,$os) = split(':'); $nums{$name} = $num; $stats{$name} = $stat; $oses{$name} = $os; $names[$i] = $name; $i++; } close CSV; #make necessary changes to %nums, %stats, %oses as required, or delete + an entry entirely # write data over $csv open (CSV, '>', $csv) or die; flock(CSV, 2); foreach my $name(@names) { print CSV join(':',$name,$nums{$name},$stats{$name},$oses{$name}). +"\n"; } close CSV;
There are more efficient ways, of course, but this will get the job done.

Replies are listed 'Best First'.
Re: Re: How to modify/delete records in a flat-file database?
by pelagic (Priest) on Apr 28, 2004 at 12:40 UTC
    > How might I ensure that my original data remains uncorrupted even with multiple users?
    Unless there are really multiple updates at a time ...

      Oops, I missed that. It certainly brings more complexity to the problem... Database would definitely be the way to go then, I'd think. Even though I lock the files when they are read and written in my example above, two concurrent users could overwrite each other's changes.
        Thank you for sharing your code example with me. Thats a really nice way to read in data; much cleaner than the way that I would have thought. Regarding what was said about the multiple updates, I'm not sure how much concurant useage would occur with this script. I guess it's best to play it safe, however. Thank you very much for your input. Joe

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2022-12-04 22:05 GMT
Find Nodes?
    Voting Booth?

    No recent polls found