Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How to process multiple input files?

by jaredor (Deacon)
on May 22, 2011 at 20:49 UTC ( #906200=note: print w/ replies, xml ) Need Help??


in reply to How to process multiple input files?

Try using the while construct with the <> operator. Something like

while (my $line = <>) { ... }

Oops, after submission I saw jwkrahn responded in more detail. That comment should solve (both) your problems, which I now understand to be 1) looping over command line file names, and 2) Modifying the second line of each file. One thing you might do instead of maintaining your own counter would be to use the built-in line counter. The special $. line number variable will be properly maintained from file to file. (will not be properly maintained with the <> operator unless you take special steps as described in the link given. Thank you again jwkrahn.)


Comment on Re: How to process multiple input files?
Download Code
Re^2: How to process multiple input files?
by John M. Dlugosz (Monsignor) on May 23, 2011 at 00:27 UTC
    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.

      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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://906200]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (14)
As of 2014-07-22 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (129 votes), past polls