Could probably be optimized a little better, but that should work for you just fine. Just set the $max_records to however many records (not kb) you want in each file. Set the file path at the top, and there are two spots where you need to specify filename (the input file, and the output file. On the output file, dont remove the $num portion.)
Code:
#!/usr/bin/perl -w
use strict;
print "Content-type: text/html\n\n";
my $max_records = 1000; # Maximum number of records in each file
my $file_path = '/path/to/directory/where/file/is';
my @elements;
my $count = 0;
my $file_no = 1;
open (FILE, "$file_path/test.dat") or die "Can't open file. Reason: $!";
while (<FILE> ) {
push (@elements, $_);
$count++;
if ($count == $max_records) {
write_file($file_no, @elements);
$file_no++;
@elements = ();
$count = 0;
}
}
close (FILE) or die "Can't close file. Reason: $!";
write_file($file_no, @elements) if @elements; # Write the leftovers
sub write_file {
my ($num, @items) = @_;
print "Writing File #$num...\n";
open (FILE, ">$file_path/output$num.dat") or die "Can't open file. Reason: $!";
print FILE foreach @items;
close (FILE) or die "Can't close file. Reason: $!";
print "Finished Writing File #$num...\n\n";
}
--mark
[This message has been edited by Mark Badolato (edited April 28, 2000).]