1

我有两个包含以下数据的 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
4

1 回答 1

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
于 2012-11-17T08:38:48.283 回答