Arrays 使用Perl将一个制表符分隔文件中的数组元素替换为另一个文件中的哈希值
我想用相应的散列值替换数组中的每个元素。更清楚地说:我有两个文件1)ref.tab 2)data.tab。 参考文件包含以下数据:Arrays 使用Perl将一个制表符分隔文件中的数组元素替换为另一个文件中的哈希值,arrays,perl,hash,substitution,Arrays,Perl,Hash,Substitution,我想用相应的散列值替换数组中的每个元素。更清楚地说:我有两个文件1)ref.tab 2)data.tab。 参考文件包含以下数据: A a B b C c D d 1 apple red A 2 orange orange B 3 grapes black C 4 kiwi green D 数据文件包含以下数据: A a B b C c D d 1 apple
A a
B b
C c
D d
1 apple red A
2 orange orange B
3 grapes black C
4 kiwi green D
数据文件包含以下数据:
A a
B b
C c
D d
1 apple red A
2 orange orange B
3 grapes black C
4 kiwi green D
现在我想使用Perl做的是:用ref.tab中的相应值替换data.tab第4列中的所有值实例
我的代码如下:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
# Define file containing the reference values:
open DFILE, 'ref.tab' or die "Cannot open data file";
# Store each column to an array:
my @caps;
my @small;
while(<DFILE>) {
my @tmp = split/\t/;
push @caps,$tmp[0];
push @small,$tmp[1];
}
print join(' ', @caps),"\n";
print join(' ', @small),"\n";
# convert individual arrays to hashes:
my %replaceid;
@replaceid{@caps} = @small;
print "$_ $replaceid{$_}\n" for (keys %replaceid);
# Define the file in which column values are to be replaced:
open SFILE,'output.tab' or die "Cannot open source file";
# Store the required columns in an array:
my @col4;
while(<SFILE>) {
my @tmp1 = split/\t/;
push @col4,$tmp1[4];
}
for $_ (0..$#col4) {
if ($_ = keys $replaceid[$col4[$_]]){
~s/$_/values $replaceid[$col4[$_]]/g;
}
}
print "@col4";
close (DFILE);
close (SFILE);
exit;
解决办法是什么
新发行:
现在是另一个问题。如果没有相应的替换项,我想将该字段留空。你知道怎么做吗?就是
参考标签
A a
B b
C c
D d
F f
data.tab:
1 apple red A
2 orange orange B
3 grapes black C
4 kiwi green D
5 melon yellow E
6 citron green F
期望输出:
1 apple red a
2 orange orange b
3 grapes black c
4 kiwi green d
5 melon yellow
6 citron green f
我该怎么做
新一期,2
我现在有另一个关于解决方案的问题。如果没有匹配项,它会将字段留空,但在第4行之后会有额外的列;因此,每当没有找到匹配项时,第五列中的值就会移到第四列
1 apple red a sweet
2 orange orange b sour
3 grapes black c sweet
4 kiwi green d sweet
5 melon yellow sweet
6 citron green f sour
第五行:在这里你可以看到发生了什么;第5列中的值移到第4列,在该列中未找到替换项。第4列中的值是
$tmp1[3]
,而不是$tmp1[4]
use strict;
use warnings;
# Define file containing the reference values:
open my $DFILE, '<', 'ref.tab' or die $!;
my %replaceid;
while (<$DFILE>) {
my ($k, $v) = split;
$replaceid{$k} = $v;
}
close $DFILE;
# print "$_ $replaceid{$_}\n" for (keys %replaceid);
# Define the file in which column values are to be replaced:
open my $SFILE, "<", 'data.tab' or die $!;
local $" = "\t"; #"
while(<$SFILE>) {
my @tmp1 = split;
$tmp1[3] = $replaceid{ $tmp1[3] } // qq{"no '$tmp1[3]' key in \$replaceid!"};
# tab separated output of @tmp1 array, thanks to $" var set above
print "@tmp1\n";
}
close $SFILE;
使用严格;
使用警告;
#定义包含参考值的文件:
打开我的$DFILE,'Perl解决方案:
- 我们完全读取ref.tab文件,并将其加载到散列中,其中列1作为键,列2作为值
- 读取ref.tab文件后,我们移动到data.tab文件,并用哈希值替换第4列
ref.tab输出:A B C D dNo。它们由新行(即A(制表符空间)A(新行)B(制表符空间)B等分隔on@biobudhan,顺便说一句,perl-lne'print“|$| |”ref.tab的输出是什么?perl-lne'print“|$| |”ref.tab的输出是:| A | | | B |是的,A\ta由B\tb新行分隔。不要说“我出错了”。总是说“这是我得到的错误”,然后告诉我们确切的错误。不要解释它。不要重新打字。完全从您的屏幕上剪切并粘贴错误消息。@AndyLester:谢谢您的更正。下一次我会关注它的!:-)你还没有告诉我们错误是什么。@AndyLester:我没有得到想要的输出。相反,我一直在replace.pl第4行的join或string中使用未初始化的值$tmp1[3]。我编辑了您的问题以显示错误。
awk 'NR==FNR{a[$1]=$2;next}{$4=(a[$4])?a[$4]:""}1' OFS="\t" ref.tab data.tab