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 ) {

You are assigning 0 to $cnt so $cnt will always be false and the else block will always execute.


    $my file = "File_" . $subset ;
    open CSV , ">" , "$file" ;
    print CSV "$_[ $_ ]" ;

Why are you using the @_ array? Array indexing starts at 0 but your foreach loop starts at 1 so why are you skipping over $_[0]?


  }
   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.

I would do something like this:

for my $subset ( 1 .. 2 ) {
    open my $CSV, '>', "File_$subset"
        or die "Cannot open 'File_$subset' $!";
    print $CSV join ',', @_[ ($subset-1)*3000 .. $subset*3000-1 ];
    }



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to