There are unfortunately several issues with your code:
- You're using the second capture group as the filename, which according to the regex is actually the third word in the line. You probably meant something like /^zone\s+(\w+)\W+\w+\s*$/, and then you can use the capture variable $1.
- As jcb mentioned, you've got a stray > in the regex.
- sprintf($outfh'.txt',... is not valid syntax. Please make sure to always provide a runnable SSCCE when asking questions.
- I don't understand why you have a $filecount variable, since according to your description, you want the filenames to be derived from just the section names.
- Using the variable $outfh to hold both the filename and the filehandle is not good practice, you should use a separate variable to hold the filename.
- You print to the output file unconditionally, even when it's not open yet. The same goes for the close.
- You say you want the output files for this example input to contain six lines each, but you don't say which of the seven input lines.
If I fix these issues (keeping $filecount), I get:
use warnings;
use strict;
my $infn = 'testdat.dat';
open(my $infh, '<', $infn) or die "$infn: $!";
my $outfh;
my $filecount = 0;
while ( my $line = <$infh> ) {
if ( $line =~ /^zone\s+(\w+)\W+\w+\s*$/ ) {
close $outfh if $outfh;
my $outfn = sprintf '%s-%d.txt', $1, ++$filecount;
open($outfh, '>', $outfn) or die "$outfn: $!";
}
if ($outfh) {
print {$outfh} $line or die "print: $!";
}
}
close($outfh) if $outfh;
close($infh);
However, this produces output files that are 7 lines or longer, because they include the endoffile marker and any lines following it. If you want the endoffile to be excluded from the output, the fix is fairly simple:
if ( $line =~ /^zone\s+(\w+)\W+\w+\s*$/ ) {
...
}
elsif ( $line =~ /^endoffile$/ ) {
close $outfh;
$outfh = undef;
}
If you want something more complex than this, then it'd be better to switch to a state machine type approach, which I showed some templates for at the top of this node.
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.