【发布时间】:2013-08-22 23:34:00
【问题描述】:
我是一个全新的 Perl 新手,在我的第一个 Perl 脚本方面寻求帮助
我有一些 30-50GB 的大文件,它们是这样构造的 - 数百万列和数千行:
A B C D E 1 2 3 4 5 6 7 8 9 10
A B C D E 1 2 3 4 5 6 7 8 9 10
A B C D E 1 2 3 4 5 6 7 8 9 10
A B C D E 1 2 3 4 5 6 7 8 9 10
A B C D E 1 2 3 4 5 6 7 8 9 10
A B C D E 1 2 3 4 5 6 7 8 9 10
A B C D E 1 2 3 4 5 6 7 8 9 10
我想删除“A”列和“C”列,然后是数字列的三分之一,所以“3”列和“6”列,然后是“9”列,直到最后文件。空格分隔。
我的尝试是这样的:
#!/usr/local/bin/perl
use strict;
use warnings;
my @dataColumns;
my $dataColumnCount;
if(scalar(@ARGV) != 2){
print "\nNo files supplied, please supply file name\n";
exit;
}
my $Infile = $ARGV[0];
my $Outfile = $ARGV[1];
open(INFO,$Infile) || die "Could not open $Infile for reading";
open(OUT,">$Outfile") || die "Could not open $Outfile for writing";
while (<INFO>) {
chop;
@dataColumns = split(" ");
$dataColumnCount = @dataColumns + 1;
#Now remove the first element of the list
shift(@dataColumns);
#Now remove the third element (Note that it is now the second - after removal of the first)
splice(@dataColumns,1,1); # remove the third element (now the second)
#Now remove the 6th (originally the 8th) and every third one thereafter
#NB There are now $dataColumnCount-1 columns
for (my $i = 5; $i < $dataColumnCount-1; $i = $i + 3 ) {
splice($dataColumns; $i; 1);
}
#Now join the remaining elements of the list back into a single string
my $AmendedLine = join(" ",@dataColumns);
#Finally print out the line into your new file
print OUT "$AmendedLine/n";
}
但我遇到了一些奇怪的错误:
- 这是说它不喜欢我在 for 循环中的 $1,我添加了一个“我的”,这似乎使错误消失了,但其他人的 for 代码似乎在这里包含一个“我的”,所以我不是确定发生了什么。
全局符号“$i”需要在 Convertversion2.pl 第 36 行显示包名。 全局符号 "$i" 需要在 Convertversion2.pl 第 36 行显示包名。 全局符号 "$i" 需要在 Convertversion2.pl 第 36 行显示包名。 全局符号“$i”需要在 Convertversion2.pl 第 36 行显示包名。
- 另一个错误是这样的: Convertversion2.pl 第 37 行的语法错误,“@dataColumns;”附近 Convertversion2.pl 第 37 行,“1)”附近的语法错误
我不知道如何纠正这个错误,我想我快到了,但不确定语法错误到底是什么,不确定如何修复它。
提前谢谢你。
【问题讨论】:
-
您在写
splice行时有点脑残。首先,$dataColumns应该是一个数组@dataColumns。其次,您应该在列表中使用逗号,而不是分号。您可以只使用数组切片,而不是使用拼接,例如print OUT join(" ", @dataColumns[1,3,4,5,6,8,9]) -
@TLP:对于 “百万列”,切片并不完全实用!
-
$dataColumnCount在拼接时变得越来越不准确。你知道@#dataColumns代表什么吗?splice的结果如何影响for循环的增量子句的有效性? -
@Borodin 创建要保留的索引列表就像创建要删除的索引列表一样容易。而且它肯定比用循环和诸如此类的方式来处理拼接更可取。
-
@TLP:啊,所以你想更多的是
print join(' ', @dataColumns[@keep]), "\n"。也许。我很想知道这对于大型@keep是否有效。