in reply to File parsing query

Greetings all,
Right off the bat I would think of using seek and the $. special variable (current line number: see Perl Special Variables Quick Reference) to keep track of where you have been and where you want to print to.

-InjunJoel

Update!
Though it seemed simple at first, upon actually attempting to code something using seek and $. I agree with Animator. What I got was 40+ lines of buggy code before I finally stopped. Disregard my posting above.
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

Replies are listed 'Best First'.
Re^2: File parsing query
by Animator (Hermit) on Feb 16, 2005 at 20:57 UTC

    First note: adding in the middle of a file is impossible (overwritting is possible).

    Second note: In my post above I did not use $. (I did think about it though), because the OP speaks about widgets, so I guess that a widget is a special kind of line, but I can be wrong ofc...

    Seek and tell in this case would be hard to use.

    It would be possible to use it when the following steps are followed:

    • A new temp file is created,
    • The original file is opened for reading,
    • File is read line by line
    • If line matches the Database-string:
      • Print to temp-file
      • Print a fixed amount of (empty) bytes to the file, store the starting address of this data in a var (by using tell)
      • Go to the previous point where empty-bytes where added (with seek) and overwrite them with the correct data.
      • Jump back to the place where we were. (using seek)
    • If it didn't match, check for widget etc and print to temp file.

    Some remarks by this story: how much bytes should be reserved for the string?

    • preferably it would be exactly the number of bytes that are needed. This could be accomplished by storing the number as an integer instead of ASCII for example,
    • If too few bytes are added then vital information is overwritten,
    • If too much bytes are added then the file is filled with empty data.

    Using seek and tell would be possible if and only if a fixed number of bytes are added, so you can open a new (temp), write data to it while you read, add some empty bytes (which will be overriden at a later point) and do the ma