On Dec 22, 2003, at 4:32 PM, [EMAIL PROTECTED] wrote:


I have two text files I would like to open and read at the same time.
Whats the best way to do this?

the only problem with your current form, barring the faux paux of

        while( ($line1 = <TESTFILE>) && ($line2 = <LOGFILE>))
        {

}

is what to do with the case of one file being longer than
the other file.

So you might try the idea of

        my ($line1, $line2 , $flag );
        while ( 1 ) {
                $line1 = <TESTFILE>; # read them each
                $line2 = <LOGFILE>;
                last unless ( $line1 || $line2 ); # if both empty exit loop
                #
                # otherwise test for which one just finished
                #
                unless( $line1 )
                {
                        $flag = 1;
                        last;
                }
                unless( $line2 )
                {
                        $flag = 2;
                        last
                }
                #
                # now do the voodo on the two lines
                #
                chomp($line2);
                chomp($line1);
                print "$line2 : $line1\n";
        }
        # if one needs to know which ended first one
        # can deal with that here...
        if ( $flag )
        {
                print "more lines to read\n";
                print "\tfrom TestFile \n" if ( $flag == 1 );
                print "\tfrom logfile \n" if ( $flag == 2 );
        } else {
                print "both ended together\n";
        }
        # now one would be safe to close the files

HTH.

ciao
drieux

---


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to