I believe I understand your question. (disclaimer: this in no way is an admission of liability if I do not, in fact, understand your question)

What I think you should do (for simplicity's sake) is run 2 passes on the file, like so:

#use whateveryouwant; my $filename = "insertfilenamehere"; open INPUT, $filename or die; my $modify_state = "false"; my $line = ""; while ($line = <INPUT>) { chomp($line); #look for the value if ($line =~ /are these the droids you're looking for?/) { $modify_state = "true"; last; } } close INPUT; if ($modify_state eq "false") { open INPUT, $filename; open OUTPUT, ">output" or die; while ($line = <INPUT>) { #modify the line here #and print to the output file } close OUTPUT; close INPUT; #rename the output file back to the original filename. }

Notes about the code: It's not particularly efficient. You might have to go through the file twice (worst case). However to me the advantage is that it is simple. You look for the value. if you find it then stop looking and start over modifying the file. If you dont find it then you are already done. Hope this helps.

Final note. This code has not been tested and it will take a little work to address your problem. It is intended as a framework for you to use.


In reply to Re: redo entire loop by zek152
in thread redo entire loop by Anonymous Monk

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.