Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: reading files to different output files.

by LanX (Saint)
on May 28, 2017 at 13:24 UTC ( [id://1191427]=note: print w/replies, xml ) Need Help??


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

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!

  • Comment on Re^3: reading files to different output files.

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

    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;

        the input file should come from the command line. i don't want to determine the file within the script. Doesn't <> within the while loop open the files the users enters in the command line? If not, how can i implement the open command in my script?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-18 01:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found