in reply to Re: reading files to different output files.
in thread reading files to different output files.

is there any solution using this approach:

use IO::File; my @fh; my @file_names = ("output1.txt", "output2.txt", "output3.txt"); for (my $i=0; $i<=$#file_names; $i++){ $fh[$i]= IO::File->new( ">$file_names[$i]" ) || die "Cannot open $ +file_names[$i]: $!.\n"; }

Replies are listed 'Best First'.
Re^3: reading files to different output files.
by choroba (Cardinal) on May 28, 2017 at 17:04 UTC
    Or, without any modules:

    my @fhs = map { open my $FH, '<', $_ or die $!; $FH } @filenames;

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re^3: reading files to different output files.
by LanX (Saint) on May 28, 2017 at 13:24 UTC
    What exactly is your problem now?

    Did you try it? And what went wrong?

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      yes, I tried it this way:

      #! /usr/bin/perl use strict; use warnings; use IO::File; my @fh; my @file_names = ("output1.txt", "output2.txt", "output3.txt"); while ( <> ){ for (my $i=0; $i<=$#file_names; $i++){ $fh[$i]= IO::File->new( ">$file_names[$i]" ) || die "Cannot op +en $file_names[$i]: $!.\n"; print {$fh[$i]} "$_"; } } print "done.\n";

      this script produces three output files, each contains the input filename that the user writes into the command line. Do I have to somehow open the input files in the script?

        Do I have to somehow open the input files in the script?

        Yes.

        open my $input_file, '<', $input_filename or die "Cannot open input fi +le $input_filename: $!.\n"; while (my $input_line = <$input_file>) { chomp $input_line; print "[$input_line]\n"; } close $input_file;