【问题标题】:compare values of different tables perl比较不同表的值perl
【发布时间】:2014-11-24 08:37:36
【问题描述】:

我有两个制表符分隔的表格:

table1

col1    col2    col3    col4
id1     1       1       10
id2     1       15      20
id3     1       30      35
id4     2       10      15


table2

col1    col2    col3
rs1     5       1
rs2     11      1
rs3     34      1
rs4     35      1

我首先要检查 col3-table2 和 col2-table1 中的值是否匹配。如果这是 TRUE,那么我想检查 col2-table2 中是否有介于 col3 和 col4 - table1 中的值之间的值。如果是这种情况,我想将 col1 和 col2 的相应值打印到 table1 的新列中。

所以在本例中,最终结果文件应如下所示:

 table output
 col1    col2   col3   col4   new_col1    
 id1     1      1      10     rs1:5
 id2     1      15     20     
 id3     1      30     35     rs3:34, rs4:35    
 id4     2      10     15

打开并加载文件后,我开始将 table2 的值存储在数组数组中。

my @table2;
    while (<$table2>){
        next if /^\s*#/; #to skip header lines
        my @columns = split;
        next if $columns[1] =~ /\D/;
        push @table2, \@columns;
        }

while (<$table1>){
     my @columns = split;
     ... 
}

我现在如何检查 col3-table2 和 col2-table1 中的值是否匹配。以及如何继续检查 col2-table2 中的值是否介于 col3 & col4 - table1 中的值之间。

【问题讨论】:

标签: perl


【解决方案1】:

幸运的是,我的记事本中还有上次的代码。

根据更改的要求,我进行了一些更新。这应该按照您的要求进行。 (在没有内联的情况下输入表格数据留给读者作为练习)。

use strict;
use warnings;
use Data::Dumper;

my %table2;

while (<DATA>) {

    #stop reading if we've finished with table2
    last if m/^table1/;

    next unless m/^rs/;
    my ( $col1, $col2, $col3 ) = split(/\s+/);
    $table2{$col1}{$col3} = $col2;
}

print "The story so far...:\n";
print Dumper \%table2;


print "table output\n";
print join( "\t", qw ( col1    col2   col3   col4   new_col1 ) ), "\n";
while (<DATA>) {
    next unless m/^id/;
    chomp;
    my ( $rowid, $col2, $lower, $upper ) = split(/\s+/);
    my $newcol = "";
    foreach my $rs ( keys %table2 ) {
        if ( defined $table2{$rs}{$col2}
            and $table2{$rs}{$col2} >= $lower
            and $table2{$rs}{$col2} <= $upper )
        {
            $newcol .= " $rs:$table2{$rs}{$col2}";
        }
    }
    print join( "\t", $rowid, $col2, $lower, $upper, $newcol, ), "\n";
}


__DATA__
table2
col1    col2    col3
rs1     5       1
rs2     11      1
rs3     34      1
rs4     35      1

table1
col1    col2    col3    col4
id1     1       1       10
id2     1       15      20
id3     1       30      35
id4     2       10      15

这给出了以下输出:

The story so far...:
$VAR1 = {
          'rs3' => {
                     '1' => '34'
                   },
          'rs4' => {
                     '1' => '35'
                   },
          'rs2' => {
                     '1' => '11'
                   },
          'rs1' => {
                     '1' => '5'
                   }
        };

table output
col1    col2    col3    col4     new_col1
id1     1       1       10       rs1:5
id2     1       15      20  
id3     1       30      35       rs3:34 rs4:35
id4     2       10      15  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多