#!perl use strict; use warnings; use List::MoreUtils 'uniq'; use Text::CSV_XS; my %files = (file1 => '1.csv', file2 => '2.csv'); my %hashes; my $csv = Text::CSV_XS->new( { binary => 1 } ); for my $file (keys %files) { open(my $in, '<', $files{$file}) or die "Cannot open file '$files{$file}' for reading: $!"; <$in>; # Discard column headings while (my $row = $csv->getline($in)) { my $key = shift @$row; $hashes{$file}{$key} = [ @$row ]; } close $in or die "Cannot close file '$files{$file}': $!"; } separator_line(); print join("\t", qw(frag id1 file1 id2 file2)), "\n"; separator_line(); my @keys; push @keys, keys %$_ for values %hashes; @keys = uniq @keys; for my $fragment (sort @keys) { my $f1 = exists $hashes{file1}{$fragment} ? 1 : 0; my $f2 = exists $hashes{file2}{$fragment} ? 1 : 0; printf "%s\t%s\t%s\t%s\t%s\n", $fragment, $f1 ? $hashes{file1}{$fragment}->[0] : '', $f1, $f2 ? $hashes{file2}{$fragment}->[0] : '', $f2, } separator_line(); sub separator_line { print '-' x 37, "\n"; }