in reply to reading files to different output files.
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 $_; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: reading files to different output files.
by ic23oluk (Sexton) on May 30, 2017 at 07:40 UTC | |
by Corion (Patriarch) on May 30, 2017 at 11:08 UTC | |
by ic23oluk (Sexton) on May 31, 2017 at 14:25 UTC | |
by choroba (Cardinal) on May 31, 2017 at 14:53 UTC | |
by shmem (Chancellor) on May 31, 2017 at 15:40 UTC | |
by ic23oluk (Sexton) on May 31, 2017 at 15:18 UTC | |
by shmem (Chancellor) on May 30, 2017 at 11:46 UTC | |
by Anonymous Monk on May 30, 2017 at 11:10 UTC | |
by Anonymous Monk on May 30, 2017 at 10:50 UTC |