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 size 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;