If you use the special null filehandle for reading multiple files on the command line (i.e. if you read via <>) , the name of the current input file will show up in the special variable $ARGV (see perlvar). You can use this variable to determine whether the file name has changed because the next file has been opened, and (re-)open the output filehandle to another file. Of course you have to provide means to change the output file name (e.g. a prefix or suffix applied to the input file name, or a counter), otherwise the same file will be re-opened and clobbered.
Example:
my $filename; # used to remember the current file name my $out = "output"; # prefix for output files my $ofh; # output filehandle, currently closed while (<>) { if ($ARGV ne $filename) { $filename = $ARGV; open $ofh, '>', "$out-$filename" or die "Can't write to '$out-$filename': $!\n"; } print $ofh $_; }
In reply to Re: reading files to different output files.
by shmem
in thread reading files to different output files.
by ic23oluk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |