On Wednesday 24 December 2008, Mr. Shawn H. Corey wrote:
> 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( 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
> }
This worked great, thank you. I had to modify the print line to include
$_, for whatever reason it created blank files as written. I changed it
to:
print $out_fh "$_" or die "could not print to $file: \n"
which did what I was hoping for. Thanks again. :-)
--
------------------------------------------------------------------------
Charles Farinella
Appropriate Solutions, Inc. (www.AppropriateSolutions.com)
[email protected]
voice: 603.924.6079 fax: 603.924.8668
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/