Kevin Viel wrote:
I have a file with thousands of fields, but a subsequent program can only
had 3000. I have to break this file up, for simplicity:
00001-03000
03001-06000
Hopefully, a basic outline of my code will suffice to get informative
advice.
my $cnt = -1 ;
my $subset = 1 ;
foreach ( 1..6000 ){
$cnt++ ;
if ( $cnt = 0 ) {
$my file = "File_" . $subset ;
open CSV , ">" , "$file" ;
print CSV "$_[ $_ ]" ;
}
else{
print CSV ",$_[ $_ ]" ;
}
if ( $cnt == 3000 ){
close CSV ;
$cnt = 0 ;
$subset++ ;
}
} # cycled through all of the fields
After I print the mod( $_ , 3000 ) = 0 field to the file, I close that
filehandle and reset $cnt to zero. I am not sure, but it seems that the
open function is not openning the subsequent filehandles.
I would appreciate any corrections or suggestions.
The program below does what you need. I hope it helps.
Rob
use strict;
use warnings;
my $cnt;
my $subset;
my $out;
open my $in, '<', 'input_file' or die $!;
while (<$in>) {
if (++$cnt % 3000 == 1) {
$subset++;
open $out, '>', "File_$subset" or die $!;
}
print $out $_;
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/