Hello ssinha, and welcome to the Monastery!

You are trying to modify a text file in-place, but are using the wrong method. Consider the following extract from the documentation for open:

You can put a + in front of the > or < to indicate that you want both read and write access to the file; ... You can't usually use either read-write mode for updating textfiles, since they have variable-length records.

Here are some strategies you can use:

  1. Open the data file for reading, and a different (temporary) file for writing. Process the first file line-by-line, writing each new line to the temp file. When done, rename the temp file with the name of the data file.
  2. Same as (1) but done implicitly via the -i command switch. See Command Switches.
  3. Open the file, and tie it to an array using the core module Tie::File.

Alternatively, depending on your needs, you may be well-advised to pursue the database solution recommended by sundialsvc4 above.

Hope that helps,

Athanasius <°(((><contra mundum


In reply to Re: Persistent data storage by Athanasius
in thread Persistent data storage by ssinha

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.