in reply to updating/modifying line in a file

Yes, two handles at once, like so:

open(DB_RESULT,">/path/to/result/db/file"); open(DB_SRC,"/path/to/source/db/file"); while (<DB_SRC>){ 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`;
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( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re^2: updating/modifying line in a file
by bran4ever (Novice) on Jun 04, 2006 at 08:26 UTC
    Hello again, i am at university right now, and for the TIE::FILE does nto work at all.... i tried..but i m not sure if its right i will paste this code
    open(data,"userlist.txt") or di +e "$@"; while ($line = <data>) { @details = split(/:/,$l +ine); if ($name[0] eq $detail +s[0]) { print "check -----> $de +tails[0] , $details[1] ,$downloadTotal \n"; open(FH, ">download +temp.txt") or die "$@"; print "check 2 ---- +-> $details"; print(FH $downloadT +otal); open(FH,">>userlis +t.txt") or die "$@"; $details[2] = $ det +ails[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