in reply to Re: Replace current line in while loop
in thread Replace current line in while loop

Actually, what the script does is attempts to connect to a server, if the attempt comes back successful then the timestamp should be updated to reflect todays date. However, if the attempt failed, then the timestamp (line) should be left alone. I unfortunately do not want to create a new file, rather I'd just like to update the timestamp if success, and leave the line alone if fail.
  • Comment on Re^2: Replace current line in while loop

Replies are listed 'Best First'.
Re^3: Replace current line in while loop
by tilly (Archbishop) on May 13, 2009 at 18:23 UTC
    Given this information I'd suggest storing data in some kind of database. Even if it is just a dbm file.
Re^3: Replace current line in while loop
by Marshall (Canon) on May 13, 2009 at 18:36 UTC
    As other have pointed out there are problematic details with modify in place. You could consider using Tie:File,
    http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm.

    This allows you to view file as an array and handles the details for you. The file isn't automatically all read into memory and so can work on large files. Could be some performance issues or not in your app.

    Update: As another thought, you could reconsider your approach. A common way is to just log the data by appending to a file. Then have some other program that analyzes the log data to make reports. Of course this depends upon how many lines this log file will have, etc...

    Another update: If performance is a consideration, appending to an existing file is pretty much the fastest way to write something to the disk. And this is essentially independent of the file size. Appending data to a 1K file is same cost as appending data to a 1GB file. If your program spews out say 1,000 lines per hour, that's only 24,000 lines per day. Not much data in the scheme of things, 10x that much is not that much. It sounds to me like you just need daily stats. Even on Windows there is the concept of a "chron" job and you can also start tasks at different priorities (from a .bat file, use the "start" command instead of just typing in the executable name). But I don't think you need that. I mean crunching through say a 250K line log file to make a report is easy.

    My advice is: do something simple that functionally does what you want. If that doesn't "work" for performance reasons, then get more complex.

Re^3: Replace current line in while loop
by kennethk (Abbot) on May 13, 2009 at 18:21 UTC
    Given the new information you have provided, the most reasonable approach is following tilly's advice. There is no good reason to attempt in-place editing when output-and-move is sooo much easier and less bug-prone.