http://qs321.pair.com?node_id=906219


in reply to Re: How to process multiple input files?
in thread How to process multiple input files?

He'll always have a line-count of 1, since he's slurping the files. The counter variable is used to count how many times the replacement is triggered with the /g option, not the number of "lines" read (he only reads one "line" in the original!).

Putting the declaration of $counter inside the loop should do the trick simply. A better solution might be to rewrite the regex to find the second occurrence of </div> rather than finding all of them and only substituting the second, and "inserting" the content directly rather than repeating the found stuff in the replacement.

Replies are listed 'Best First'.
Re^3: How to process multiple input files?
by jwkrahn (Abbot) on May 23, 2011 at 06:51 UTC
    He's always have a line-count of 1, since he's slurping the files.

    $. contains the current record count, and since each file is one record it will be incremented for each file and so will not always be 1.    Unless of course you reset $. or close ARGV at the end of each file.

      Thanks for pointing this out. I have learned from you too.

      Furthermore, you've inspired me to reread the links I posted. Now I'm going back to strike kill my wrong summation of those links, which did have the right info, even if I didn't read them correctly.

Re^3: How to process multiple input files?
by jaredor (Priest) on May 23, 2011 at 05:08 UTC

    Thanks for pointing out my errors. I simply did not read the code closely enough.

    All your responses in this thread were good. I learned something. Good work.