fragment id index accb 10 A bbc 11 B ccd 12 C #### fragment id index ccd 15 D llk 11 B kks 12 C #### fragment id_file file 1 id_file2 file 2 accb 10 1 0 bbc 11 1 14 1 ccd 12 1 15 1 llk 0 11 1 kks 0 12 1 #### use strict; use warnings; use feature qw(say); use autodie; use Text::CSV_XS; use constant { FILE_1 => "1.csv", FILE_2 => "2.csv", }; my %hash; # # Load the Hash with value from File #1 # open my $file1_fh, "<", FILE_1; while ( my $value = <$file1_fh> ) { chomp $value; $hash{$value}++; } close $file1_fh; # # Add File #2 to the Hash # open my $file2_fh, "<", FILE_2; while ( my $value = <$file2_fh> ) { chomp $value; $hash{$value} += 10; # if the key already exists, the value will now be 11 # if it did not exist, the value will be 10 } close $file2_fh; open my $file3_fh, "<", FILE_3; while ( my $value = <$file3_fh> ) { chomp $value; $hash{$value} += 100; } close $file3_fh; for my $k ( sort keys %hash ) { if ($hash{$k} == 1) { # only in file 1 say "$k\t1\t0"; } elsif ($hash{$k} == 10) { # only in file 2 say "$k\t0\t1"; } else { # in both file 1 and file 2 say "$k\t1\t1"; } } open (OUT, ">final.csv") or die "Cannot open OUT for writing \n"; $, = " \n"; print OUT "fragment\tid_file\tfile1\tid_file2\tfile2\n\n"; print OUT (sort keys %hash); close OUT;