> -----Original Message----- > From: John W. Krahn [mailto:[EMAIL PROTECTED] > Sent: Thursday, January 10, 2008 8:26 AM > To: Perl Beginners > Subject: Re: open and close a filehandle > > 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.
Correct, this should be ==, not =. > > > $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]? An error. The file format is ID, status, SNP1,..., SNPn. ID ($_[ 0 ]) must appear in each of the subset files. > > } > > 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 ]; > } This should greatly improve my program. I will just have to determine how many subsets and, for the last, the last marker (SNP). I apologize for the slop. I always use warnings and strict (per Chas's reply). I code on my Solaris box but e-mail on my windows machine, which typically has more distractions, and my son was bugging me to leave work... Thank you, Kevin Kevin Viel, PhD Post-doctoral fellow Department of Genetics Southwest Foundation for Biomedical Research San Antonio, TX 78227 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/