input - output

Discussion in 'Perl Misc' started by Gunnar Hjalmarsson, Mar 26, 2005.

  1. nicolas-laurent wrote:
    > 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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 26, 2005
    #1
    1. Advertising

  2. 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
    nicolas-laurent, Mar 26, 2005
    #2
    1. Advertising

  3. * nicolas-laurent schrieb:
    >
    > 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
    Fabian Pilkowski, Mar 26, 2005
    #3
  4. Gunnar Hjalmarsson

    Big and Blue Guest

    nicolas-laurent wrote:

    > 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.



    --
    Just because I've written it doesn't mean that
    either you or I have to believe it.
    Big and Blue, Mar 26, 2005
    #4
  5. nicolas-laurent wrote:
    > 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
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Mar 26, 2005
    #5
  6. In an excellent answer Fabian Pilkowski wrote:

    > 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.
    Brian McCauley, Mar 27, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    12
    Views:
    1,598
  2. ashutosh
    Replies:
    3
    Views:
    8,978
    Andrew Thompson
    Jun 16, 2005
  3. Guy
    Replies:
    5
    Views:
    2,814
    brucie
    Dec 13, 2003
  4. cjl
    Replies:
    1
    Views:
    10,491
    Michael Winter
    Oct 31, 2005
  5. Replies:
    1
    Views:
    467
Loading...

Share This Page