![]() |
|
more useful options | |
PerlMonks |
Feedback request for text file manipulatorby NovMonk (Chaplain) |
on Mar 25, 2004 at 14:31 UTC ( #339743=perlquestion: print w/replies, xml ) | Need Help?? |
NovMonk has asked for the wisdom of the Perl Monks concerning the following question:
Greetings, Esteemed Monks, I have been playing around today with a little program that will read in a series of lines from an input file (using __DATA__ below for simplicity), use some things on those lines to create some new lines, and write the whole thing back out to a new file. So for an input file like this:
I can get this:
Suppose I now want it to be a little smarter. I want it to look at how many letters/ spaces are before the ; on each n01 line, and make the g and p lines conform to that, so I have instead:
and I want to keep the whole thing from getting over a page width of 142 characters. Here's my code thus far. I'd appreciate comments on how I could make what I'm currently doing better, and on whether my thoughts below about how I want to tackle my new objective are anywhere near the right track. Note, I'm not looking for solutions, but feedback on what I've got and where I might look next to build on my understanding.
I'm thinking first I want to know how many n01 lines there are (if over a certain number, I probably want to restrict each ----- to something like 5 or 6 characters). To get that, I'm thinking I might read the input file into an array and count the n01 lines as they go by, then have my while loop above run through that array. Otherwise, I want to know how many characters are in each $ntext as they are read, and adjust the number of characters for $gtext and $ptext accordingly. I think getting the number of characters would be something like and then using $x as the length of my strings for all 3 variables, which would then look something like this:
I realize that will require me to change how I'm dealing with $gtext and $ptext, but that doesn't seem too difficult. I imagine if the $ntext is over a certain length, I'll want to truncate it, same as if there are too many n01 lines. I suspect that will be putting some subroutines in my future, if nothing else. I'm sure there are dozens of nifty modules that could do all this for me, but my aim is to do a bit of learning as I reinvent the wheel. I'm sure it's obvious that I'm very new to this-- but I hope it's also apparent that I'm progressing. Thanks as always for the wise admonishments. NovMonk
Back to
Seekers of Perl Wisdom
|
|