in reply to Re: Read two files and print
in thread Read two files and print

Hi, How to extract only second field from the text1.txt
open(FILE1, 'text1.txt'); open(FILE2, 'text2.txt'); while ($line = <FILE1>) { @lines=split("\t",$line); print $lines[1]; if($lines[1] != <FILE2>){ # print "$line"; } }
I tried something like this. But split doesnot works here. How can I check <FILE2> with second column of text1.txt

Replies are listed 'Best First'.
Re^3: Read two files and print
by velusamy (Monk) on Feb 26, 2009 at 04:27 UTC
    Hi,
    Try this..
    open(FILE1, 'File1.txt'); open(FILE2, 'File2.txt'); while ($line = <FILE1>) { @lines=split(' ',$line); print $lines[1]; if($lines[1] != <FILE2>){ print "$line"; } }

    In split use space as a delimiter, that will exactly splits the line.

      What do you expect if($lines[1] != <FILE2>){ to do?

      Always use strictures (use strict; use warnings;)!

      != will unmify the strings on either side of it, most likely to 0, which is hardly what you might desire. != provides a scalar context for <FILE2> so a single line will be read from FILE2 for each iteration through the while loop, regardless of any synchronization issue that may be caused by extra or missing lines in either file.


      True laziness is hard work