in reply to spliting large file into smaller

open(my $in, '<', "filein.dat") or die("Unable to open filein.dat: $!\n"); my @buf; my $file_count = 0; while (<$in>) { push(@buf, $_); if (@buf == 30) { open(my $fh_out, '>', "file${file_count}.dat") or die("Unable to create file${file_count}.dat: $!\n"); print $fh_out ($_) foreach @buf; @buf = (); ++$file_count; } } if (@buf) { open(my $fh_out, '>', "file${file_count}.dat") or die("Unable to create file${file_count}.dat: $!\n"); print $fh_out ($_) foreach @buf; }

Update: Changed
print($fh_out)
to
print $fh_out ($_)

Replies are listed 'Best First'.
Re^2: spliting large file into smaller
by ikegami (Patriarch) on Nov 10, 2005 at 23:41 UTC

    Here's a simpler version:

    open(my $fh_in, '<', "filein.dat") or die("Unable to open filein.dat: $!\n"); my $fh_out; my $file_count = 0; while (<$fh_in>) { if ($. % 30 == 1) { open($fh_out, '>', "file${file_count}.dat") or die("Unable to create file${file_count}.dat: $!\n"); ++$file_count; } print $fh_out ($_); }

    The redundancy was eliminated. The buffer was eliminated.

    You might need a close before the open.