4

我是一个全新的 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";
}

但我收到了一些奇怪的错误:

  1. 它是说它不喜欢我在 for 循环中的 $1,我添加了一个“我的”,这似乎使错误消失但没有其他人的代码似乎在这里包含一个“我的”,所以我不确定是什么继续。

全局符号“$i”需要在 Convertversion2.pl 第 36 行显式包名。全局符号“$i”需要在 Convertversion2.pl 第 36 行显式包名。全局符号“$i”需要在 Convertversion2.pl 第 36 行显式包名. 全局符号 "$i" 需要在 Convertversion2.pl 第 36 行显示包名。

  1. 另一个错误是:Convertversion2.pl 第 37 行的语法错误,靠近“@dataColumns;” Convertversion2.pl 第 37 行,“1)”附近的语法错误

我不确定如何纠正这个错误,我想我快到了,但不确定语法错误到底是什么,不确定如何修复它。

先感谢您。

4

3 回答 3

3

在我就这个问题发表博客后,一位评论者指出,我的测试用例可以减少 45% 的运行时间。我稍微解释一下他的代码:

my @keep;
while (<>) {
    my @data = split;

    unless (@keep) {
        @keep = (0, 1, 0, 1, 1);
        for (my $i = 5; $i < @data; $i += 3) {
            push @keep, 1, 1, 0;
        }
    }

    my $i = 0;
    print join(' ', grep $keep[$i++], @data), "\n";
}

这几乎是我原来解决方案所用时间的一半:

$ time ./zz.pl input.data > /dev/null
真正的 0m21.861s
用户 0m21.310s
系统 0m0.280s

现在,以一种相当肮脏的方式使用Inline::C可以获得另外 45% 的性能:

#!/usr/bin/env perl

use strict;
use warnings;

use Inline C => <<'END_C'

/*
  This code 'works' only in a limited set of circumstances!
  Don't expect anything good if you feed it anything other
  than plain ASCII 
*/

#include <ctype.h>

SV *
extract_fields(char *line, AV *wanted_fields)
{
    int ch;
    IV current_field = 0;
    IV wanted_field = -1;

    unsigned char *cursor = line;
    unsigned char *field_begin = line;
    unsigned char *save_field_begin;

    STRLEN field_len = 0;
    IV i_wanted = 0;
    IV n_wanted = av_len(wanted_fields);

    AV *ret = newAV();
    while (i_wanted <= n_wanted) {
        SV **p_wanted = av_fetch(wanted_fields, i_wanted, 0);
        if (!(*p_wanted)) {
            croak("av_fetch returned NULL pointer");
        }
        wanted_field = SvIV(*p_wanted);

        while ((ch = *(cursor++))) {

            if (!isspace(ch)) {
                continue;
            }

            field_len = cursor - field_begin - 1;
            save_field_begin = field_begin;
            field_begin = cursor;

            current_field += 1;
            if (current_field != wanted_field) {
                continue;
            }

            av_push(ret, newSVpvn(save_field_begin, field_len));
            break;
        }
        i_wanted += 1;
    }
    return newRV_noinc((SV *) ret);
}

END_C
;

而且,这里是 Perl 部分。请注意,我们split只计算一次要保留的字段索引。一旦我们知道了这些,我们将线和(基于 1 的)索引传递给 C 例程以进行切片和切块。

my @keep;
while (my $line = <>) {
    unless (@keep) {
        @keep = (2, 4, 5);
        my @data = split ' ', $line;
        push @keep, grep +(($_ - 5) % 3), 6 .. scalar(@data);
    }
    my $fields = extract_fields($line, \@keep);
    print join(' ', @$fields), "\n";
}
$ time ./ww.pl input.data > /dev/null
真正的 0m11.539s
用户 0m11.083s
系统 0m0.300s

input.data使用以下方法生成:

$ perl -E 'say join(" ", "A" .. "ZZZZ") for 1 .. 100' > input.data

它的大小约为 225MB。

于 2013-08-23T00:19:58.610 回答
2

您显示的代码不会产生这些错误。你根本没有$1,如果你的意思是$i那么你使用那个变量是好的。唯一的语法错误是在splice($dataColumns; $i; 1)有分号而不是逗号的行中,并且使用$dataColumns而不是@dataColumns.

除此之外

  • 最好的做法是声明变量尽可能靠近它们的使用点,而不是在程序的顶部。

  • 大写字母通常用于包名称等常量。您应该对变量使用小写字母、数字和下划线。

  • 您是否知道您设置$dataColumnCount的元素数量比 中的元素数量多@dataColumns

  • 最近不赞成使用全局文件句柄 - 您应该改用词法变量。

我建议对您的程序进行重构。它用于autodie避免检查open调用是否成功。它构建了一个需要尽快删除的数组索引列表:一旦在读取第一条记录后知道每行中的字段数。然后它从末尾向后删除它们,以避免在删除前面的元素时必须对索引进行算术运算。

#!/usr/local/bin/perl

use strict;
use warnings;
use autodie;

if (@ARGV != 2) {
  die "\nNo files supplied, please supply file names\n";
}

my ($infile, $outfile) = @ARGV;
open my $info, '<', $infile;
open my $out,  '>', $outfile;

my @remove;

while (<$info>) {

  my @data = split;

  unless (@remove) {
    @remove = (0, 2);
    for (my $i = 7; $i < @data; $i += 3) {
      push @remove, $i;
    }
  }

  splice @data, $_, 1 for reverse @remove;

  print $out join(' ', @data), "\n";
}
于 2013-08-23T00:13:04.033 回答
0

虽然上面的其他答案完美无缺,而我的可能没有任何优势,但这是实现相同目标的不同方式,同时避免split

#!/usr/local/bin/perl
use strict;
use warnings;
use feature 'say';

my $dir='D:\\';
open my $fh,"<", "$dir\\test.txt" or die;

while (<$fh>) {
    chomp;
    my @fields = split ' ';
    print "$fields[0] $fields[2] ";
    for (my $i=7; $i <= $#fields; $i += 3){
        print "$fields[$i] ";
    }
    print "\n";
}
close $fh;

如果这没用,请告诉我。

于 2014-04-04T07:46:26.543 回答