in reply to updating/modifying line in a file

If your data store is SQL92 compliant, or even if DBI is willing to think it is, this is easily accomplished with a query of the form below. I've never used them, but as I understand it, DBI has a data base driver that will read a csv file and probbaly has others for other flat file formats, including colon delimited.

my $sql = "UPDATE tablename SET bandwidth = bandwidth + ? WHERE user = + ?"; my $sth = $dbh->prepare($sql); my $sth->execute($newtraffic,$user);
However, if you are reading and writing a plain text file, and don't want to wire it up through DBI, you could also grep on $user to find your line, then use something like:

my @user_fields = split /:/,$_; $user_fields[2] = $user_fields[2] + $newtraffic; my $newline = $user_fields[0].':'.$user_fields[1].':'.$user_fields[2];
then you can open two file handles, one for reading and one for writing, read the data from the original file, writing it to the new file, using the code above wrapped in a conditional checking for your $user (if(m/$user/){}), to massage it as it goes by.

-- Hugh

if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re^2: updating/modifying line in a file
by bran4ever (Novice) on Jun 04, 2006 at 07:58 UTC
    open two file handles at the same time?