I have many csv file. I want to compare particular filds of all file(columns: fragmnet, id, index) of that i wat to compare only fragment field of every files with each other. and in output i wat (colums:fragment, id_file1,file1(1 if present or 0),id_file2,file2(i if present or 0) etc.). i wrote the code in hash but it require file with 1 comlumn only.

file1

fragment id index accb 10 A bbc 11 B ccd 12 C

file2

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;

In reply to comparing csv files in perl by ray15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.