ray15 has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: comparing csv files in perl
by Athanasius (Cardinal) on Sep 27, 2014 at 03:24 UTC | |
|
Re: comparing csv files in perl
by blindluke (Hermit) on Sep 26, 2014 at 18:07 UTC | |
|
Re: comparing csv files in perl
by GotToBTru (Prior) on Sep 26, 2014 at 18:25 UTC |