Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 如果在一列中复制值,则将另一列中的值复制到上面的一行_Perl - Fatal编程技术网

Perl 如果在一列中复制值,则将另一列中的值复制到上面的一行

Perl 如果在一列中复制值,则将另一列中的值复制到上面的一行,perl,Perl,我正在用一张像这样的桌子工作 C1 C2 C3 1 a b 2 c d 4 e g 4 f h 5 x y ... ... ... C1 C2 C3 1 a b 2 c d 4 e,f g,h 5 x y 如果C1中的值与我希望将C2和C3的值粘贴在C1中有4的第一行上的值相同(在本例中是4的两倍),我希望删除C1中有4的

我正在用一张像这样的桌子工作

C1    C2    C3
1     a     b
2     c     d
4     e     g
4     f     h
5     x     y
...   ...   ...
C1    C2    C3
1     a     b
2     c     d
4     e,f   g,h
5     x     y
如果C1中的值与我希望将C2和C3的值粘贴在C1中有4的第一行上的值相同(在本例中是4的两倍),我希望删除C1中有4的第二行。所以最后应该是这样的

C1    C2    C3
1     a     b
2     c     d
4     e     g
4     f     h
5     x     y
...   ...   ...
C1    C2    C3
1     a     b
2     c     d
4     e,f   g,h
5     x     y
我正在使用perl脚本。我正在使用while循环浏览文件。我在其他脚本中使用过我的%seen或count,但我不知道如何使用它们。看起来做起来很简单

这就是我的while循环目前的样子

 while (<$DATA>) {
    @columns = split
    $var1 = $columns[0]
    $var2 = $columns[1]
    $var3 = $columns[2];         
     }  
while(){
@列=拆分
$var1=$columns[0]
$var2=$columns[1]
$var3=$columns[2];
}  

使用散列来控制重复项。在我的示例中,我使用了一个散列(
%info
)和键C1和C2。它们中的每一个都包含一个数组引用,用于添加重复项

use strict;
use warnings;

my %info = ();
while (<DATA>) {
    my @columns = split /\s+/;
    if( exists $info{ $columns[0] } ) {
        push @{ $info{ $columns[0] }->{C2} }, $columns[1];
        push @{ $info{ $columns[0] }->{C3} }, $columns[2];
    }
    else {
        $info{ $columns[0] } = { C2 =>[ $columns[1] ], C3 => [ $columns[2]] }
    }        
}  

foreach my $c1(sort {$a<=>$b} keys %info ) {
    print $c1, "\t", 
          join(',',@{$info{$c1}->{C2}}), "\t", 
          join(',',@{$info{$c1}->{C3}}), "\n";
} 


__DATA__
1     a     b
2     c     d
4     e     g
4     f     h
5     x     y
使用严格;
使用警告;
我的%info=();
而(){
my@columns=split/\s+/;
if(存在$info{$columns[0]}){
推送{$info{$columns[0]}->{C2}},$columns[1];
推送{$info{$columns[0]}->{C3},$columns[2];
}
否则{
$info{$columns[0]}={C2=>[$columns[1]],C3=>[$columns[2]]}
}        
}  
foreach my$c1(排序{$a$b}键%info){
打印$c1,“\t”,
join(“,”,@{$info{$c1}->{C2}),“\t”,
连接(“,”,@{$info{$c1}->{C3}),“\n”;
} 
__资料__
1 a b
2cd
4 e g
4 f h
5 x y