input - output

G

Gunnar Hjalmarsson

nicolas-laurent said:
This script takes in1.txt, in2.txt in3.txt form the command line and
print this on the screen.

while (<>) {
for $a (split) {print "$a -- file $ARGV\n"}
}

result:
word -- file in1.txt
word --- file in1.txt
word -- file in2.txt
word -- file in2.txt
etc.

Question: How to generate output files for each input file that
contains the result of the script ?

out_in1.txt
word -- file in1.txt
word --- file in1.txt

out_in2.txt
word -- file in2.txt
word -- file in2.txt

What have you tried?

perldoc -f open
 
N

nicolas-laurent

This script takes in1.txt, in2.txt in3.txt form the command line and
print this on the screen.

while (<>) {
for $a (split) {print "$a -- file $ARGV\n"}
}

result:
word -- file in1.txt
word --- file in1.txt
word -- file in2.txt
word -- file in2.txt
etc.


Question: How to generate output files for each input file that
contains the result of the script ?

out_in1.txt
word -- file in1.txt
word --- file in1.txt

out_in2.txt
word -- file in2.txt
word -- file in2.txt

Thanks
 
F

Fabian Pilkowski

* nicolas-laurent said:
This script takes in1.txt, in2.txt in3.txt form the command line and
print this on the screen.

while (<>) {
for $a (split) {print "$a -- file $ARGV\n"}
}

Question: How to generate output files for each input file that
contains the result of the script ?

out_in1.txt
word -- file in1.txt
word --- file in1.txt

out_in2.txt
word -- file in2.txt
word -- file in2.txt

If you can change your suggestion about your filenames, you can use the
command line switch »-i« to edit your files in-place. E.g. after calling
your script with

$ perl -iin_* script.pl in1.txt in2.txt in3.txt

the files in1.txt, in2.txt etc contain the desired output, and the newly
created files in_in1.txt, in_in2.txt etc are backups of the input files.
If you don't need backups, supply the »-i«-switch without any extension.
Btw, the switch can be mentioned in your script's shebang line too.

If your filenames matter, try to open and write your files manually.

regards,
fabian
 
B

Big and Blue

nicolas-laurent said:
Question: How to generate output files for each input file that
contains the result of the script ?

Basic code is:

while (<>) {
if ($. == 1) {
print $ARGV, "\n";
}
for $a (split) {print "$a -- file $ARGV\n"}
close ARGV if (eof);
}

Look up open and eof (in perlfunc) and ARGV (in perlvar) for why.
 
J

John W. Krahn

nicolas-laurent said:
This script takes in1.txt, in2.txt in3.txt form the command line and
print this on the screen.

while (<>) {
for $a (split) {print "$a -- file $ARGV\n"}
}

result:
word -- file in1.txt
word --- file in1.txt
word -- file in2.txt
word -- file in2.txt
etc.


Question: How to generate output files for each input file that
contains the result of the script ?

out_in1.txt
word -- file in1.txt
word --- file in1.txt

out_in2.txt
word -- file in2.txt
word -- file in2.txt

while ( <> ) {
open OUT, ">out_$ARGV" and select OUT if 1..1;
print "$_ -- file $ARGV\n" for split;
}



John
 
B

Brian McCauley

In said:
If you can change your suggestion about your filenames, you can use the
command line switch »-i« to edit your files in-place. E.g. after calling
your script with

$ perl -iin_* script.pl in1.txt in2.txt in3.txt

the files in1.txt, in2.txt etc contain the desired output, and the newly
created files in_in1.txt, in_in2.txt etc are backups of the input files.

Actually they are not newly created, they are the original files renamed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top