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
open(DB_RESULT,">/path/to/result/db/file"); open(DB_SRC,"/path/to/source/db/file"); while (There may be some slick way to do this with only one file open overwriting only the line you are concerned with, but it is not apparent to me at this late hour. And generally I am of the mind that it is more important to ensure that a program works than that it is fast or fancy.){ if($_ ~= /$user/){ my @user_fields = split /:/,$_; $user_fields[2] = $user_fields[2] + $newtraffic; my $newline = $user_fields[0].':'.$user_fields[1].':'.$user_fields[2]; print DB_RESULT, $newline; } else { print DB_RESULT, $_; } } close(DB_SRC); close(DB_RESULT); my $result = `mv /path/to/result/db/file /path/to/source/db/file`;
open(data,"userlist.txt") or die "$@";
while ($line = )
{
@details = split(/:/,$line);
if ($name[0] eq $details[0])
{
print "check -----> $details[0] , $details[1] ,$downloadTotal \n";
open(FH, ">downloadtemp.txt") or die "$@";
print "check 2 -----> $details";
print(FH $downloadTotal);
open(FH,">>userlist.txt") or die "$@";
$details[2] = $ details[2] + $downloadTotal;
$newline = $details[0].':'.$details[1].':'.$details[2];
print(FH $newline);
}
i checked the userlist.txt
its like this after this:
kevin:12345678:
dex:12345678:11955436
brandon:12345678:kevin:12345678:12348kevin:12345678:134kevin:12345678:135
CountZero
"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
perlmonks.org content © perlmonks.org and bran4ever, CountZero, hesco, mickeyn, sgifford
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03