下面是我的脚本。我已经尝试了许多打印语句来弄清楚为什么它仅访问第一个数组元素。模式匹配有效。该数组至少包含40个元素。我已经检查了,已经满了。我已经打印了每一行,并且打印了每一行。
my $index = 0;
open(FILE, "$file") or die "\nNot opening $file for reading\n\n";
open(OUT, ">$final") or die "Did not open $final\n";
while (<FILE>) {
foreach my $barcode (@barcode) {
my @line = <FILE>;
foreach $_ (@line) {
if ($_ =~ /Barcode([0-9]*)\t$barcode[$index]\t$otherarray[$index]/) {
my $bar = $1;
$_ =~ s/.*//;
print OUT ">Barcode$bar"."_"."$barcode[$index]\t$otherarray[$index]";
}
print OUT $_;
}
$index++;
}
}
好的,可以说输入是:
File:
Barcode001 001 abc
Barcode002 002 def
Barcode003 003 ghi
@barcode holds:
001
002
003
@otherarray holds:
abc
def
ghi
该脚本的输出结果当前仅打印:
条码001_001 abc
它应该在打印:
>Barcode001_001 abc
>Barcode002_002 def
>Barcode003_003 ghi
应打印约40行的整个负载。
有任何想法吗?我访问数组元素的方式一定有问题吗?还是递增?希望这不是太愚蠢!提前致谢。
它需要索引,因为我正在尝试按顺序并行匹配数组。每行都需要将数组的相应索引与文件中的每行匹配。
原来这是一个简单的问题,因为我怀疑是星期一。我有一位同事和我一起经历了它,这就是索引的位置:
#my $index = 0; #This means the index is iterated through,
#but for each barcode for one line, then it continues
#counting up and misses the other values, therefore
#repeatedly printing just the first element of the array.
open(FILE, "$file") or die "\nNot opening $file for reading\n\n";
open(OUT, ">$final") or die "Did not open $final\n";
while (<FILE>) {
$index = 0; #New placement of $index for initialising.
foreach my $barcode (@barcode) {
my @line = <FILE>;
foreach $_ (@line) {
if ($_ =~ /Barcode([0-9]*)\t$barcode[$index]\t$otherarray[$index]/) {
my $bar = $1;
$_ =~ s/.*//;
print OUT ">Barcode$bar"."_"."$barcode[$index]\t$otherarray[$index]";
}
print OUT $_;
$index++; #Increment here
}
#$index++;
}
}
感谢每个人的回答,对于我本来的措辞不佳的问题,他们本来可以解决并且可能会更有效率,但是出于脚本和我编辑过的问题的目的,必须采用这种方式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句