Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have two files and I want to merge the common lines as well as print the uncommon ones. I have written a code which is working but I would like to have some better way to do it. The code I have written is :
@files = @ARGV; $size = @files; open (FA, ">>comp.txt"); open (FH, @ARGV[0]); while ($seq = <FH>) { chomp $seq; @seq = split ("\t", $seq); $seqm = @seq[0]; chomp $seqm; open (FH1, @ARGV[1]); while ($seq2 = <FH1>) { chomp $seq2; @seq2 = split ("\t", $seq2); $seqm1 = @seq2[0]; chomp $seqm1; if ($seqm1 =~m/$seqm/) {$names=$names."\n".$seq."\t".@seq2[1]."\n"; $c=1; last;} else {$c=0;} } if ($c=="0") {$names=$names."\n".$seq."\n";} } open (FH, @ARGV[1]); while ($seq = <FH>) { chomp $seq; @seq = split ("\t", $seq); $seqm = @seq[0]; chomp $seqm; open (FH1, @ARGV[2]); while ($seq2 = <FH1>) { chomp $seq2; @seq2 = split ("\t", $seq2); $seqm1 = @seq2[0]; chomp $seqm1; if ($seqm1 =~m/$seqm/) { $c=1; last;} else {$c=0;} } if ($c=="0") {$names1=$names1."\n".$seq."\n";} } print FA $names."\n".$names1;

Replies are listed 'Best First'.
Re: find unique lines in two files
by Linicks (Scribe) on Oct 28, 2016 at 12:34 UTC

    If you are on a linux base, why re-invent the wheel?

    man uniq

    Nick

      I think this is the best answer. Better to use an existing solution than to program it yourself. You can also use a series of greps or a diff as an alternative to uniq, depending on exactly what you are trying to do. Check out this relevant link.

Re: find unique lines in two files
by hippo (Archbishop) on Oct 28, 2016 at 12:36 UTC
Re: find unique lines in two files
by Laurent_R (Canon) on Oct 28, 2016 at 18:03 UTC
    Yes there is a "better way to do it": please do yourself a favor: format your code consistently and indent it correctly.

    I am sorry, I am doing this type of file comparison all the time and I might have some ideas from my experience, but I am not going to take the time to try to understand your code if you can't take the time to format it properly.

    Please don't take this as as personal offense, correct formatting of the code is really essential.

Re: find unique lines in two files
by tybalt89 (Monsignor) on Oct 28, 2016 at 21:10 UTC

    You say "two files" but open a third:

    open (FH1, @ARGV[2]);

    What's that about?