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

first of all, thank you for your effort :) I wrote another program and I'm sure I am nearer to the solution than ever. However, it doesn't do anything:

#! usr/bin/perl use strict; use warnings; use IO::File; my @out; my @file_names = ("output1.txt", "output2.txt", "output3.txt"); my $i = 0; while (<>){ foreach (@ARGV){ open (READ, $ARGV) || die "Cannot open $ARGV: $!.\n"; $out[$i] = IO::File->new( ">file_names[$i]" ) || die "Cannot o +pen file_names[$i]: $!.\n"; $i++; while ( <READ> ){ print {$out[$i]} "$_"; } } }

so what's the problem?

Replies are listed 'Best First'.
Re^5: reading files to different output files.
by choroba (Cardinal) on May 31, 2017 at 14:53 UTC
    Where do you use @file_names ?

    foreach (@ARGV) sets $_ to the script arguments, where do you use $_ ?

    ($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^5: reading files to different output files.
by shmem (Chancellor) on May 31, 2017 at 15:40 UTC

    Do you get an error message? If so, why don't you post it?

    so what's the problem?

    The problem is that you are getting things mixed up. So several problems:

    • You loop foreach (@ARGV) and use $ARGV instead of $_ in the inner loop.
    • You assign to $out[$i], then increment $i, then access $out[$i] - which is undefined. Bummer. You need not stuff the filehandle into @out, since you are only using one output filehandle each time through. Use a lexical filehandle e.g. my $outfh = IO::File->new(...)
    • You are missing a $ in the expression ">file_names[$i]" - that should be ">$file_names[$i]". Same in the subsequent argument to <c>die.</c>

    Having fixed that:

    while (<>){

    This loop reads all files on the commandline line by line, file after file and executes the code enclosed in the loop body, for every line from all files.

    Inside the while loop, you iterate over all files again with the foreach(ARGV) inner loop, opening and reading them. You open, read, write all files over and over as many times as all the files have lines, always incrementing $i which gets you quickly past the end of @file_names.

    Drop the outer while (<>) loop.

    update: But why am I writing you, at all? You didn't give a damn at Re: reading files to different output files., so what...

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'