Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PERL Problem - Need Urgent Help

Options
  • 15-06-2005 11:47am
    #1
    Moderators, Society & Culture Moderators, Sports Moderators Posts: 12,272 Mod ✭✭✭✭


    Hi,

    I'm in the middle of doing my college 6 month internship with a company and I had to write a PERL script to do something. Problem is I have never used PERL before. However my script is working perfectly bar some very minor problems. Could somebody please help me to do this:

    I am writing various records to 3 text files. Each text file contains a header at the top of the file which among other things contains a record count. Problem is I need to write this header first into the file as I am extracting the records from XML files which need to be entered one by one on the command line by a user. This means that I can only get a true record count after all the XML files required have been entered.

    I dont know how to go back to the point at the end of my header and write the record count into the file. Even if there was a way to write at the start of the file I could re-write the entire header into the file. At the moment I am writing in the count manually but this wont do as the script needs to be distributed among other people in my company.

    Does anybody know how I could get a count and then go back and write in into the file?
    Or can I leave the header until last and then add in a new line at the start of the file for the header which will push the original first line down one?
    Any help would be much appreciated.
    Thanks


Comments

  • Moderators, Society & Culture Moderators, Sports Moderators Posts: 12,272 Mod ✭✭✭✭Kingp35


    Ok I have managed to "seek" to the offset where I need to write in the record count and it seems to work ok when the count is a single digit. When its a double digit I think the secong digit is being written over my newline character because the line underneath the header ends up on the same line as the header.

    Is there a way of seeking to a point in a file and writing data to the file at that point without overwriting data thats already there?

    So close to getting it to work now


  • Registered Users Posts: 1,186 ✭✭✭davej


    You could define a fixed length for your recordcount information, say 4 bytes which would allow you to count up to a suitably large number . Then, no matter what the record count is you will always need to write 4 bytes to the offset position.

    davej


  • Registered Users Posts: 2,426 ✭✭✭ressem


    You're right, the newline will be overwritten.
    Think of the existing file as a block. To insert something in the middle, increasing it's length, you've to move all following bytes by that many chars.

    Easy choices, knowing nothing about the requirements of the header or file size:
    a reserve space in the file header, e.g. Make the number a constant length padded with spaces or zeros. Leave leeway. (if it's to be in text form you need more than 4 digits)
    b create a temporary buffer/file. While copying the contents, do your insert then finish copy. Write to disk.
    http://bumppo.net/lists/macperl-anyperl/1999/08/msg00062.html


  • Moderators, Society & Culture Moderators, Sports Moderators Posts: 12,272 Mod ✭✭✭✭Kingp35


    I think the temp file method may work.

    Thanks for the info guys


  • Registered Users Posts: 2,426 ✭✭✭ressem


    I read the FAQ, suggested using
    Tie::File - Access the lines of a disk file via a Perl array

    using splice()

    http://www.perladvent.org/2002/21st/


  • Advertisement
  • Moderators, Society & Culture Moderators, Sports Moderators Posts: 12,272 Mod ✭✭✭✭Kingp35


    Thanks for all your help my script works perfectly now.

    Much appreciated


Advertisement