NovMonk has asked for the wisdom of the Perl Monks concerning the following question:
While I was mopping the chapel floor this morning after Matins (or was it Lauds?) I had an epiphany. Actually I have this one quite often-- it is the nagging suspicion that I am doing something simple very much the long way 'round. I have a laughably simple program which is making my job much easier, but I think it can and should be better.
I have a number of *.txt files in directory, and I am doing something to each of them in turn. I currently use a file called "qcode" that lists each of the files I need processed, without the *.txt extension, thus:
q2
q5
q7
q23
What I am wondering is, is there another way to get at this, if I know that all the *txt files in the directory are the ones I want to process? At the end of this process I get these same *txt files saved as *.axe files with my changes. Here's my code-- as always, comments are appreciated.
#!/usr/bin/perl use strict; use warnings; my ($qnum,$code,$ctext,$ntext,$stext); open (QLIST, "qcode") || die "Can't open qcode file: $!\n"; while (<QLIST>){ chomp $_; $qnum="$_"; open (QIN, "$qnum.txt") || die "Can't open $qnum.txt file: $! +\n"; open (QOUT, ">$qnum.tmp") || die "Can't open $qnum.tmp file: +$!\n"; while (<QIN>){ s/\222/'/g; s/\226/-/g; chomp ($_); if (/^Col/i){ s/^col.//gi; s/\(net\)//gi; $ntext = $_; print QOUT "net1$ntext (net);unl1\n"; } elsif (/^\(S/i){ (undef,$stext) = split(/\s/,$_,2); print QOUT "net2$stext (subnet);unl1\n"; } elsif (/^[0-9]/) ($code,$ctext) = split(/\s/,$_,2); print QOUT "n01$ctext;c=(&txt($code.ge.1)\n"; } else {}; } } close QIN; close QOUT; open (QIN, "$qnum.tmp") || die "Can't open $qnum.tmp file: $!\n"; open (QOUT, ">$qnum.axe") || die "Can't open $qnum.axe file: $!\n"; while (<QIN>){ open (QIN, "$qnum.tmp") || die "Can't open $qnum.tmp file: $!\n"; + open (QOUT, ">$qnum.axe") || die "Can't open $qnum.axe file: $!\n +"; while (<QIN>){ if (/n01\s+nothing|n01\s+don't know|n01\s+other/i) { s/\n/;nz;nosort\n/g;} if (/ \s+[\w;]/){ s/n01\s+/n01/g; s/net1\s+/net1/g; s/net2\s+/net2/g;} if (/^net1misc/i){ s/unl1/unl1;nz;nosort/g;} unless (/^n01;c=|^net. /i){ print QOUT $_; } } close QOUT; }
One final note-- another question really. When I run this with "use warnings" I get a gazillion warnings reading: "Use of uninitialized value in concatenation (.) or string at oeaxe.p line 23, <QIN> line 784." I don't have any idea what this means, and my output files look fine, so I'm considering just commenting this line out for when other people use the program. Should I be more worried about this?
Thanks as always for the enlightenment.
Pax vobiscum,
NovMonk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: There is more than one way (and mine is not the best)
by Belgarion (Chaplain) on May 25, 2004 at 21:22 UTC | |
|
Re: There is more than one way (and mine is not the best)
by cLive ;-) (Prior) on May 25, 2004 at 22:02 UTC | |
by revdiablo (Prior) on May 26, 2004 at 16:44 UTC | |
|
Re: There is more than one way (and mine is not the best)
by punkish (Priest) on May 25, 2004 at 23:36 UTC | |
by graff (Chancellor) on May 26, 2004 at 04:43 UTC | |
|
Re: There is more than one way (and mine is not the best)
by graff (Chancellor) on May 26, 2004 at 05:29 UTC |