Hello monks quick question from a beginning perlist: I have a program that goes through an input file line for line and modifies this input using regexes. That part is working great EXCEPT I need to use a different regex on the LAST LINE only. After hours trying different setups (i.e. banging head on desk), I have decided to seek assistance. Here's what I have thus far:
open(TXTIN,"ARGV[0]") || die "Cannot open the data file";
open(TXTOUT,">ARGV[1]") || die "Cannot open the formatted file";
#$itercnt=0
while(<TXTIN>) {#counting number of lines in input file
$itercnt++
}
for ($match=0;$match<$itercnt;$match++){
while(TXTIN){
chomp $_;
$_ =~ s/yada/yadda/;
print TXTOUT $_;
}}
#for (;$match=$itercnt;$match++){
# while(TXTIN){
# chomp $_;
#$_ =~ s/different/regex here/;
#print TXTOUT $_;
#}} # getting stuck in infinite loop in here somewhere
# so I haven't been able to see if it is working
close(TXTIN);
close(TXTOUT);
So the input file can have anywhere from 1 to 100 lines, I'll never know before hand what the number will be. I've already tried 'eof' and it doesn't work because by the time it is 'true' the last line has already been modified/written. So, (A) am I going about this the totally wrong way? (B) if so, is there a better way to do this? Please keep in mind I am a novice, and would like to at least understand what is going on. And if you see any glaring defects feel free to point them out. TIA-
Joe
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.