It may be easier to think about if you turn the logic around: first, write some code that identifies when the record type changes. Then, when that happens, and your output file has reached the maximum size, you close your current output file and open the new one.
Also, don't use stat to check the current file size, since you don't need to go to the file system to find that out, use tell instead.
use warnings; use strict; # this just generates an example input file, not part of the solution open my $tfh, '>', 'input.txt' or die $!; print $tfh "$_\n" x (rand(3)+1) for 'A'..'Z'; close $tfh; # now split the input file my $MAXSIZE = 20; open my $ifh, '<', 'input.txt' or die $!; my $outcount=0; open my $ofh, '>', 'out000.txt' or die $!; my $prev; while (<$ifh>) { chomp; if (defined $prev && $prev ne $_) { print STDERR "Record type switched from $prev to $_, output si +ze is ".tell($ofh)."\n"; if (tell($ofh)>$MAXSIZE) { close $ofh; my $ofn = sprintf 'out%03d.txt', ++$outcount; print STDERR "Opening new output file $ofn\n"; open $ofh, '>', $ofn or die $!; } } print $ofh $_, "\n"; $prev = $_; } close $ofh; close $ifh;
In reply to Re: Large File Splitter
by Anonymous Monk
in thread Large FIle Splitter
by insta.gator
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |