Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: reading files to different output files.

by choroba (Cardinal)
on May 28, 2017 at 11:32 UTC ( [id://1191421]=note: print w/replies, xml ) Need Help??


in reply to reading files to different output files.

Use an array of output file handles. You can use an array of input file handles, too.

Note that

print $file_handles[$i] $line;

doesn't work, you need to use curlies:

print {$file_handles[$i]} $line;

($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,

Replies are listed 'Best First'.
Re^2: reading files to different output files.
by ic23oluk (Sexton) on May 28, 2017 at 13:14 UTC

    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"; }
      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,
      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?

Re^2: reading files to different output files.
by ic23oluk (Sexton) on May 28, 2017 at 13:50 UTC

    yes, but it didn't work. here's my entire script:

    #! /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";

    It produced three output files, each containing the command that i entered in the command line. What's missing?

      Why use <> if you allready have the array list?

        I am in very early stages and not even familiar with the basics, maybe thats why. could someone please improve my script? would be very pleased.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1191421]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found