我有两个包含以下数据的csv文件:
gt1/sd2 34 3
gt1/sd3 67 4
gt1/sd4 20 -9
和第二个带有数据的csv文件:
gt1/sd2 9 6
gt1/sd5 10 8
gt1/sd9 21 10
现在我想比较每个csv的第一列,看看它们是否相同,然后将csv列打印到一个csv中。
我尝试将这些文件读入哈希。我的代码现在看起来像这样。当发现两个哈希值相等时,我有点困难如何编写输出csv。
my %hash1;
while (my $line = <file1> ) {
$line =~ s/\s*\z//;
my @array = split /,/, $line;
my $key = shift @array;
$hash1{$key} = \@array;
}
my %hash2;
while (my $line1 = <file2> ) {
$line1 =~ s/\s*\z//;
my @array1 = split /,/, $line1;
my $key1 = shift @array1;
$hash2{$key1} = \@array1;
}
while (my ($k,$v)=each %hash1){
for ( keys %hash1 ) {
unless ( !exists $hash2{$_} ) {
print "$_: found in second hash\n";
next;
}
print "$k $v\n"
}
}
对于此示例,输出csv应具有以下内容:
gt1/sd2 34 3 gt1/sd2 9 6
gt1/sd3 67 4 NotFound NotFound NotFound
gt1/sd4 20 -9 NotFound NotFound NotFound
答案 0 :(得分:4)
最好使用Text::CSV
来解析CSV文件:
#!/usr/bin/env perl
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new( { allow_whitespace => 1, sep_char => "\t" } );
open my $fh1, '<', 'file1' or die "Error opening file: $!";
open my $fh2, '<', 'file2' or die "Error opening file: $!";
my %key;
while ( my $row = $csv->getline($fh2) ) {
$key{ $row->[0] } = [ $row->[1], $row->[2] ];
}
while ( my $row = $csv->getline($fh1) ) {
my $gt = $row->[0];
my @columns;
if ( exists $key{$gt} ) {
@columns = ( @$row, $gt, @{ $key{$gt} } );
}
else {
@columns = ( @$row, ('Not Found') x 3 );
}
printf "%-10s\t%d\t%d\t%-10s\t%-10s\t%s\n", @columns;
}
gt1/sd2 34 3 gt1/sd2 9 6
gt1/sd3 67 4 Not Found Not Found Not Found
gt1/sd4 20 -9 Not Found Not Found Not Found