Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: How to modify/delete records in a flat-file database? by TwistedGreen
in thread How to modify/delete records in a flat-file database? by JoeJaz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2022-12-04 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?