how to read from more than one files at a time

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default how to read from more than one files at a time

    Hi,

    I need to read from more than one file at a time and do some operation on
    the strings and join them together, and put into one output file.

    Here is the code. But I noticed the second file never get read. It must be
    sth very simple to overcome this. Can anyone give me a hint?

    thanks,
    Ben

    # Usage: perl jointfiles.pl infile1 infile2 outfile
    open (IN1, $ARGV[0]);
    open (IN2, $ARGV[1]);
    open (OUT, ">$ARGV[2]");
    print "Input fileis : $ARGV[0], $ARGV[1]\n";
    print "Output file: $ARGV[2]\n";

    while(<IN1>)
    {
    chomp;
    $in1=$_;
    <IN2>;
    $in2=$_;
    printf OUT "$in1$in2\n";
    }
    close(IN1);
    close(IN2);
    close(OUT);

    Perldiscuss - Perl Newsgroups And Mailing Lists Guest

  2. Similar Questions and Discussions

    1. If you've got a bit of time can you read this and helpme out
      I'm sending this email to my lecturer, but he might not get it at the weekend. The hand in is on Friday and I need to get things moving by them. ...
    2. How can I read PDF, Excel, PPT, Doc files?
      How can I read PDF, Excel, PPT, DOC files?
    3. CS - where did formats -> other go, to read .CMX files?
      I have som clipart cds from Corel and the files are .cmx. I used to be able to open them in Illustrator as fully editable vector-files, but it...
    4. ASP to read the IIS log files
      Hi I am wanting to use ASP to read the IIS log files on our web server. Can anyone help? Thanks WD
    5. HELP - ASP to read the IIS log files
      Hi I am wanting to use ASP to read the IIS log files on our web server. Can anyone help? Thanks WD
  3. #2

    Default RE: how to read from more than one files at a time

    > -----Original Message-----
    > From: [email]benjamin.zhou@mbusa.com[/email] [mailto:benjamin.zhou@mbusa.com]
    > Sent: Wednesday, October 15, 2003 11:42 AM
    > To: [email]beginners@perl.org[/email]
    > Subject: how to read from more than one files at a time
    >
    >
    > Hi,
    >
    > I need to read from more than one file at a time and do some
    > operation on
    > the strings and join them together, and put into one output file.
    >
    > Here is the code. But I noticed the second file never get
    > read. It must be
    > sth very simple to overcome this. Can anyone give me a hint?
    >
    > thanks,
    > Ben
    >
    > # Usage: perl jointfiles.pl infile1 infile2 outfile
    > open (IN1, $ARGV[0]);
    > open (IN2, $ARGV[1]);
    > open (OUT, ">$ARGV[2]");
    > print "Input fileis : $ARGV[0], $ARGV[1]\n";
    > print "Output file: $ARGV[2]\n";
    >
    > while(<IN1>)
    > {
    > chomp;
    > $in1=$_;
    > <IN2>;
    > $in2=$_;
    $_ is not automatically assigned the input in this case.
    try:
    $in2 = <IN2>;
    > printf OUT "$in1$in2\n";
    You won't need the trailing \n, because you aren't chomping it from file IN2

    -Mark
    Mark Lobue Guest

  4. #3

    Default Re: how to read from more than one files at a time

    Perldiscuss - Perl Newsgroups And Mailing Lists wrote:
    >
    > Hi,
    Hello,
    > I need to read from more than one file at a time and do some operation on
    > the strings and join them together, and put into one output file.
    >
    > Here is the code. But I noticed the second file never get read. It must be
    > sth very simple to overcome this. Can anyone give me a hint?

    use warnings;
    use strict;
    > # Usage: perl jointfiles.pl infile1 infile2 outfile
    > open (IN1, $ARGV[0]);
    > open (IN2, $ARGV[1]);
    > open (OUT, ">$ARGV[2]");
    You should _ALWAYS_ verify that the files opened correctly.

    open (IN1, $ARGV[0]) or die "Cannot open $ARGV[0]: $!";
    open (IN2, $ARGV[1]) or die "Cannot open $ARGV[1]: $!";
    open (OUT, ">$ARGV[2]") or die "Cannot open $ARGV[2]: $!";

    > print "Input fileis : $ARGV[0], $ARGV[1]\n";
    > print "Output file: $ARGV[2]\n";
    >
    > while(<IN1>)
    > {
    > chomp;
    > $in1=$_;
    > <IN2>;
    You are reading from IN2 in a void context. In other words, you are not
    assigning the line you read from IN2 to a variable. It only gets
    assigned to $_ in a while expression like "while(<IN1>)" above.

    $in2 = <IN2>;

    > $in2=$_;
    > printf OUT "$in1$in2\n";
    > }
    > close(IN1);
    > close(IN2);
    > close(OUT);

    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  5. #4

    Default Re: how to read from more than one files at a time

    <benjamin.zhou@mbusa.com> wrote:
    >
    > I need to read from more than one file at a time and do some
    > operation on the strings and join them together, and put into
    > one output file.
    >
    > Here is the code. But I noticed the second file never get
    > read. It must be sth very simple to overcome this. Can anyone
    > give me a hint?
    >
    > thanks,
    > Ben
    >
    > # Usage: perl jointfiles.pl infile1 infile2 outfile
    use strict;
    use warnings;
    > open (IN1, $ARGV[0]);
    > open (IN2, $ARGV[1]);
    > open (OUT, ">$ARGV[2]");
    As John says,

    open IN1, $ARGV[0] or die $!;
    open IN2, $ARGV[1] or die $!;
    open OUT, ">$ARGV[2]" or die $!;
    > print "Input fileis : $ARGV[0], $ARGV[1]\n";
    > print "Output file: $ARGV[2]\n";
    >
    > while(<IN1>)
    > {
    > chomp;
    > $in1=$_;
    > <IN2>;
    > $in2=$_;
    > printf OUT "$in1$in2\n";
    > }
    You need tp think about what happens when the two files are
    different lengths. You may not expect that to happen but your
    code should still handle that case. Some symmetry between the
    handling of the two files would also be nice. Finally you
    should really use 'print' here instead of 'printf'.

    This code reads a line from each file and outputs them both
    until one or other file is exhausted.

    my ($in1, $in2);

    while (defined ($in1 = <IN1>) and defined ($in2 = <IN2>)) {
    chomp ($in1, $in2);
    print OUT "$in1$in2\n";
    }
    > close(IN1);
    > close(IN2);
    > close(OUT);
    HTH,

    Rob



    Rob Dixon Guest

  6. #5

    Default Re: how to read from more than one files at a time

    thanks John and Rob for the great enlightenment, they taught me a lot.

    I wonder why the following is wrong:
    while (($in1=<IN1>) && ($in2=<IN2>))
    {
    ...
    }

    Ben


    Rob Dixon wrote:
    > <benjamin.zhou@mbusa.com> wrote:
    > >
    > > I need to read from more than one file at a time and do some
    > > operation on the strings and join them together, and put into
    > > one output file.
    > >
    > > Here is the code. But I noticed the second file never get
    > > read. It must be sth very simple to overcome this. Can anyone
    > > give me a hint?
    > >
    > > thanks,
    > > Ben
    > >
    > > # Usage: perl jointfiles.pl infile1 infile2 outfile
    > use strict;
    > use warnings;
    > > open (IN1, $ARGV[0]);
    > > open (IN2, $ARGV[1]);
    > > open (OUT, ">$ARGV[2]");
    > As John says,
    > open IN1, $ARGV[0] or die $!;
    > open IN2, $ARGV[1] or die $!;
    > open OUT, ">$ARGV[2]" or die $!;
    > > print "Input fileis : $ARGV[0], $ARGV[1]n";
    > > print "Output file: $ARGV[2]n";
    > >
    > > while(<IN1>)
    > > {
    > > chomp;
    > > $in1=$_;
    > > <IN2>;
    > > $in2=$_;
    > > printf OUT "$in1$in2n";
    > > }
    > You need tp think about what happens when the two files are
    > different lengths. You may not expect that to happen but your
    > code should still handle that case. Some symmetry between the
    > handling of the two files would also be nice. Finally you
    > should really use 'print' here instead of 'printf'.
    > This code reads a line from each file and outputs them both
    > until one or other file is exhausted.
    > my ($in1, $in2);
    > while (defined ($in1 = <IN1>) and defined ($in2 = <IN2>)) {
    > chomp ($in1, $in2);
    > print OUT "$in1$in2n";
    > }
    > > close(IN1);
    > > close(IN2);
    > > close(OUT);
    > HTH,
    > Rob

    Perldiscuss - Perl Newsgroups And Mailing Lists Guest

  7. #6

    Default Re: how to read from more than one files at a time

    <benjamin.zhou@mbusa.com> wrote:
    >
    > thanks John and Rob for the great enlightenment, they taught me a lot.
    >
    > I wonder why the following is wrong:
    > while (($in1=<IN1>) && ($in2=<IN2>))
    > {
    > ..
    > }
    In the special case where <FH> returns a null string (when the last
    record of a file is empty and has no terminating record separator)
    that blank record will evaluate as 'false' and prematurely exit
    the loop.

    HTH,

    Rob


    Rob Dixon Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139