Ask a Question related to PERL Beginners, Design and Development.
-
Perldiscuss - Perl Newsgroups And Mailing Lists #1
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
-
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. ... -
How can I read PDF, Excel, PPT, Doc files?
How can I read PDF, Excel, PPT, DOC files? -
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... -
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 -
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 -
Mark Lobue #2
RE: how to read from more than one files at a time
> -----Original Message-----
$_ is not automatically assigned the input in this case.> 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=$_;
try:
$in2 = <IN2>;
You won't need the trailing \n, because you aren't chomping it from file IN2> printf OUT "$in1$in2\n";
-Mark
Mark Lobue Guest
-
John W. Krahn #3
Re: how to read from more than one files at a time
Perldiscuss - Perl Newsgroups And Mailing Lists wrote:
Hello,>
> 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?
use warnings;
use strict;
You should _ALWAYS_ verify that the files opened correctly.> # Usage: perl jointfiles.pl infile1 infile2 outfile
> open (IN1, $ARGV[0]);
> open (IN2, $ARGV[1]);
> open (OUT, ">$ARGV[2]");
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]: $!";
You are reading from IN2 in a void context. In other words, you are not> print "Input fileis : $ARGV[0], $ARGV[1]\n";
> print "Output file: $ARGV[2]\n";
>
> while(<IN1>)
> {
> chomp;
> $in1=$_;
> <IN2>;
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
-
Rob Dixon #4
Re: how to read from more than one files at a time
<benjamin.zhou@mbusa.com> wrote:
use strict;>
> 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 warnings;
As John says,> open (IN1, $ARGV[0]);
> open (IN2, $ARGV[1]);
> open (OUT, ">$ARGV[2]");
open IN1, $ARGV[0] or die $!;
open IN2, $ARGV[1] or die $!;
open OUT, ">$ARGV[2]" or die $!;
You need tp think about what happens when the two files are> 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";
> }
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";
}
HTH,> close(IN1);
> close(IN2);
> close(OUT);
Rob
Rob Dixon Guest
-
Perldiscuss - Perl Newsgroups And Mailing Lists #5
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
-
Rob Dixon #6
Re: how to read from more than one files at a time
<benjamin.zhou@mbusa.com> wrote:
In the special case where <FH> returns a null string (when the last>
> 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>))
> {
> ..
> }
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



Reply With Quote

