in reply to Re: compare two files on the basis of Two IDs
in thread compare two files on the basis of Two IDs

Hi, Thanks for your reply. I tried to print the result for file 1, but not for file 2. I am trying the code, but not worked for me.

#!/usr/bin/perl #use warnings; #use strict; #use Data::Dumper; my $file1 = $ARGV[0]; open($infile1,$file1); my $file2 = $ARGV[1]; open($infile2,$file2); my %file2_hash; while (my $line = <$infile2>) { next if $line =~ /^\s*$/; #skip blank lines (a common infile goof +) my ($key, $value1, $value2) = split /\s+/, $line; # use better "nam +es" I have # no idea of what a chr col + means $file2_hash{"$key:$value1:$value2"} = 1; } close $infile2; while (my $line = <$infile1>) { chomp $line; #so that output with E or M can be on same line next if $line =~ /^\s*$/; #skip blank lines (a common infile goof +) my ($chr, $value1) = split /\s+/,$line; if (exists $file2_hash{"$chr:$value1"} or exists $file2_hash{"$chr: +$value2"} ) { print "$line\tE\n"; # match exists with file 1 } else { print "$line\tM\n"; # match does NOT exist with file 1 } }

Replies are listed 'Best First'.
Re^3: compare two files on the basis of Two IDs
by marinersk (Priest) on Sep 28, 2016 at 03:05 UTC

    This code works for me. Is there something wrong with the output?

    a.dat:

    chr17 69112551 chr1 67058869 chr7 151046672 chr7 151047369 chr1 66953654

    b.dat:

    chr1 66953622 66953654 chr1 67200451 67200472 chr1 67200475 67200478 chr1 67058869 67058880 chr1 67058881 67058885 chr1 67058887 67058895

    Results:

    S:\Steve\PerlMonks>compare.pl a.dat b.dat chr17 69112551 M chr1 67058869 M chr7 151046672 M chr7 151047369 M chr1 66953654 M S:\Steve\PerlMonks>

      yes, Its a wrong code. It is giving the result with all M, but not E.. May be it is not considering the condition to print Es. Could you please help me in this. In original file, we have several entries, may give the Es.. Pleas have a look on the code

        Marshall wrote:
        "I couldn't see any way to get an "E" with your test data, so I added some extra data to my test cases. In the future, it is best if you can provide an example "desired output" that demo's the basic decisions which need to be made.
        show an example output and explain clearly how you arrived at that result.