On Wed, 2008-12-24 at 13:16 -0500, Charlie Farinella wrote: > I need to read in a file of 200 lines and print each out to a separate > file. > > I've been stumbling with this, but I don't know how to name each outfile > individually. I was hoping to see 200 files named tx1 - tx200, but > instead I get tx1234..................... for 123 files and then it dies. > Help? > > == > #!/usr/bin/perl > > use strict; > use warnings; > > my $i = 0; > my $outfile = "tx"; > > open( INFILE, "speed_test.csv" ); > > while( <INFILE> ) { > open( OUTFILE, "> $outfile" ); > print OUTFILE "$_\n"; > close OUTFILE; > $outfile = $outfile.$i++;
This appends $i to the end of the string; This is not what you specified above. > } > == #!/usr/bin/perl use strict; use warnings; my $i = 0; my $outfile = "tx"; open( my $infile_fh, '<', "speed_test.csv" ) or die "could not open speed_test.csv: $!\n"; # You should always test an open while( <$infile_fh> ) { my $file = $outfile . ( ++ $i ); open( my $out_fh, '>', $file ) or die "could not open $file: $!\n"; print $out_fh or die "could not print to $file: \n"; # prints $_ by default, $_ has a newline since it hasn't been chomp'ed close $out_fh or die "could not close $file: $!\n" # You should always test prints and closes except for STDOUT and STDERR } -- Just my 0.00000002 million dollars worth, Shawn Believe in the Gods but row away from the rocks. -- ancient Hindu proverb -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/