Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all I have a list of files to edit, but i wouldl like to print each one to same main file but with a value attached, e.g. out1 out2 ... so if my outfile handle is open(OUT, ">OUT");
print OUT $_; how can i include a one or two etc??

Replies are listed 'Best First'.
Re: print out
by ikegami (Patriarch) on Sep 23, 2004 at 20:48 UTC

    Like this???

    print OUT "one: $_" while (<ONE>); print OUT "two: $_" while (<TWO>);
      i plan on dealing with a 1000 files so not necessary suitable, perhaps a way of using a counter??

        yes:

        my $filename; my $counter; foreach $filename (@filelist) { open(IN, '<', $filename) or do { warn("Error opening $filename."); next; }; $counter++; print OUT ("${counter}: $_") while (<IN>); close(IN); }
        so output files would be named out1 out2 ... out100
Re: print out
by TedPride (Priest) on Sep 23, 2004 at 21:45 UTC
    The following will edit all the files OUT1 through OUT100. I'm not sure if that's what you wanted, though - your request was a bit unclear.
    $filename = 'OUT'; $max = 100; my ($fname, $ext) = split(/\./, $filename, 2); $ext = '.' . $ext if ($ext); my @temp; for (my $i = 1; $i <= $max; $i++) { if (!open(IN, "$fname$i$ext")) { print "Can't open $fname$i$ext fo +r read.<br>\n"; } else { @temp = <IN>; close(IN); # Do whatever editing is necessary on file data... if (!open(OUT, ">$fname$i$ext")) { print "Can't open $fname$i$ +ext for write.<br>\n"; } else { print OUT @temp; close(OUT); } } }