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++; }

In reply to Re: updating/modifying line in a file by hesco
in thread updating/modifying line in a file by bran4ever

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.