On Monday, September 8, 2003, at 09:55 AM, Akens, Anthony wrote:
Hello all...
Howdy.
I'm wanting to write a script that scans a file, ignoring all lines until it reaches a certain section, then processes all lines in that section that are not comments, until it reaches the end of that section.
Well, that doesn't sound too tough. I believe I can handle it. :)
The section would be designated like this:
## Beging Processing ##
## End Processing ##
So I open my file, and skip all lines until I see that first bit... Then do stuff until I see the last bit. Can someone help me out with this?
You've broken down the steps you need very well, right here. Now all we have to do is code them up exactly like you said...
#!/usr/bin/perl -w
use strict;
Excellent start.
open (FILE, "<myfile") or die "Could not open Template. ($!)";
This is the "open the file" step, in your list. Good.
You're missing the next step, the "skip all lines until" step. Let's add that:
while (<FILE>) { last if /^##\s+Begin Processing\s+##$/; }
while ($line = <FILE>) {
Let's make that the easier/prettier:
while (<FILE>) {
Assigning to Perl's default variable, $_.
#skip until beginning....
We already did that above.
#End when "End Processing" is reached last if $line =~ "End Processing";
This gets pretty with the default variable:
last if /^##\s+End Processing\s+##$/;
#Process the lines in between next if $line =~ /^#/;
Ditto:
next if /^#/;
#do stuff....
The final step, left as an exercise to the reader. :D
}
James
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]