- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下正则表达式:
my $scores_compiled_regex = qr{^0
\s+
(\p{Alpha}+\d*)
\s+
(\d+
\s*
\p{Alpha}*)
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s{2,}
(\d+)?
\s+
\d+ #$
}xos
;
它应该匹配像这样的行(来自一个普通的 txt 文件):
0 AAS 211 1 1 5 2 6 15
列名是:
0 INST, NAME A A- B+ B B- C+ C C- D+ D D- F CR P PR I I* W WP WF AU NR FN FS
这意味着:分数 A=1、分数 A- = 1、无分数 B+、分数 B=5 等。我正在尝试将它拆分为一个列表,而不是忽略空列,它可以工作,但是非常慢,匹配也非常慢,我的意思是慢,超过 5 秒,有时甚至更多!
文件中的前几个文件如下所示:
0 PALMER, JAN A A- B+ B B- C+ C C- D+ D D- F CR P PR I I* W WP WF AU NR FN FS TOTAL
0 ECON 103 98 35 114 1 14 75 9 35 1 10 1
分数是 A 列右侧的任何内容。
有什么想法吗?谢谢,
最佳答案
查看我的 program :
use strict;
use warnings;
# Column details and sample line, from the post
my $header = q{0 AOZSVIN, TAMSSZ B A A- B+ B B- C+ C C- D+ D D- F CR P PR I I* W WP WF AU NR FN FS};
my $sample = q{0 AAS 150 23 25 16 35 45 14 8 10 2 1 1 4 4 };
# -+--------+-----+-----+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---..
# chars 1212345678912345612345612341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234...
# num. chars: 2 9 6 6 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 *
my $unpack = q{A2A9 A6 A6 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A*};
$unpack =~ s/\s//g;
# Get column names from the "$header" variable above
my @column_names = unpack($unpack, $header);
s/\s+$// for @column_names; # get rid of trailing spaces
s/^\s+// for @column_names; # get rid of leading spaces
# Some sample data in same format, to try the script out
my @samples = (
q{0 AAS 150 23 25 16 35 45 14 8 10 2 1 1 4 4 },
q{0 AAS 353 2 3 5 2 6 1 2 },
q{0 T304 480M 3 10 8 8 2 3 2 1 1 1 },
q{0 BIOS 206 3 14 5 11 9 8 4 8 3 1 1 6 7 },
);
my @big_sample = (@samples) ;#x 200_000;
my @unpacked_data_as_arrayrefs;
m y @unpacked_data_as_hashrefs;
my $begin = time;
for my $line ( @big_sample ) {
my @data = unpack($unpack,$line);
s/\s+$// for @data; # get rid of trailing spaces
s/^\s+// for @data; # get rid of leading spaces
push @unpacked_data_as_arrayrefs, [@data]; # stop here if this is all you need
## below converts the data in a hash, based on the column names given
#my %as_hash;
#for ( 0..$#column_names ) {
# $as_hash{ $column_names[$_] } = $data[$_];
#}
#push @unpacked_data_as_hashrefs, { %as_hash };
}
my $tot = time - $begin;
print "Done in $tot seconds\n";
# verify all data is as we expected
# uncomment the ones that test hashref, if the above hashref-building code is also uncommented.
{
use Test::More;
# first sample
is($unpacked_data_as_arrayrefs[0]->[2],'AAS'); # AAS in the third column
is($unpacked_data_as_arrayrefs[0]->[7],'35'); # 35 in the 8th column
# fourth sample
is($unpacked_data_as_arrayrefs[3]->[2],'BIOS');
is($unpacked_data_as_arrayrefs[3]->[15],'6');
# sixth
is($unpacked_data_as_arrayrefs[5]->[7],'114');
is($unpacked_data_as_arrayrefs[5]->[10],'75');
done_testing();
}
它使用 unpack 根据字符串中字段的宽度(以字符为单位)将文本拆分为多个 block 。另请参阅 perlpacktut 以获取有关如何使用 unpack 进行此类字符串修改的更多详细信息。 Unpack 可能是这种格式的最佳选择,因为与正则表达式相比,它的执行速度非常快(在我的机器上大约 6 秒内解析 600_000 个这样的字符串)。
如果您需要了解程序的任何部分,请告诉我。我没有在这里发布它,因为它有点偏长(有评论总比没有好!)。如果您希望我这样做,请告诉我。
关于regex - 为什么我的 Perl 正则表达式这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4005150/
如果我的 Perl 程序使用 Perl 模块,它将如何确定在哪里可以找到包含模块代码的文件? 例如,如果程序包含: use MyModule1; # Example 1 us
我在一个文件中有一些不同格式的数字:8.3、0.001、9e-18。我正在寻找一种简单的方法来读取它们并存储它们而不会损失任何精度。这在 AWK 中很容易,但在 Perl 中是如何完成的呢?我只愿意使
我在一个文件中有一些不同格式的数字:8.3、0.001、9e-18。我正在寻找一种简单的方法来读取它们并存储它们而不会损失任何精度。这在 AWK 中很容易,但在 Perl 中是如何完成的呢?我只愿意使
我正在自学 Perl,并且在我的 Windows 8 64 位系统上安装了 Strawberry。 Strawberry 命令行似乎工作正常,我在 C 驱动器上的 Strawberry 文件夹中创建了
我在 Perl 模块 IO::Socket::SSL 中发现了一个错误,我可能会修复它,但是,我担心测试修复。我从 Debian 下载了源码包(因为我打算为它制作一个 Debian 包或补丁)并查看了
我有一个 perl 文件,它使用了两个 perl 模块 A.pm 和 B.pm。 但是在 B.pm 中我需要调用 A.pm 的子程序。即使我在 A.pm 中使用并尝试使用它,我仍然遇到未定义的错误。
有没有办法在 Perl 运行时加载整个模块?我原以为我用 autouse 找到了一个很好的解决方案,但以下代码无法编译: package tryAutouse2; use autouse 'tryAu
过去,我编写过许多 perl 模块,以及不止一些独立的 perl 程序,但我之前从未发布过多文件 perl 程序。 我有一个几乎处于 beta 阶段的 perl 程序,它将被开源发布。它需要一些数据文
我有 1 个 perl 脚本,我们在其中编写了几个子例程。例子: # Try_1.pl main(); sub main{ --- --- check(); } check { -- --} 现在,
似乎 CPAN 上的一些(很多?)模块部分是使用 XS 在 C 中实现的,如果需要,可以回退到纯 perl 实现。虽然这很聪明,但它显然会损害性能,我想知道它是否会发生,以便我可以解决问题。 有没有一
我对 perl 很陌生。我希望我可以从 perl 安装一些软件包,我这样做是这样的: perl -MCPAN -e 'install VM::EC2' 我猜它由于依赖而失败,它显示: Result:
给定一个 Perl 包 Foo.pm,例如 package Foo; use strict; sub bar { # some code here } sub baz { # more
我有一个用 Perl 编写的测试生成器。它生成连接到模拟器的测试。这些测试本身是用 Perl 编写的,并通过其 API 连接到模拟器。我希望生成的代码是人类可读的,这意味着我希望它能够正确缩进和格式化
我正在学习 Perl,非常新的用户。我可以知道这些 Perl 代码之间有什么区别吗? #!/usr/bin/perl & #!/usr/bin/perl -w 最佳答案 那不是 perl 代码,它是
我不认为这是一个重复的问题。这专门针对 Perl 模块附带的脚本。 通常,在安装多个 Perl 版本时,您可以将 perl 可执行文件标记为版本号 (perl5.32),这样它们就可以在 /whate
我有一个在文件中使用 Blowfish 加密的程序和第二个 perl 程序,它提示输入用于将其解密为字符串的密码,我希望不必将解密的源代码写入硬盘驱动器,尽管将它放在内存中并不是真正的问题,因为运行程
有没有人为 Perl 中的惰性求值列表找到了一个好的解决方案?我尝试了很多方法来改变类似的东西 for my $item ( map { ... } @list ) { } 进入懒惰的评估——例如,通
我安装了多个版本的 Perl。 我已经指定了要使用的版本。但是为了验证,我想从 .pl 脚本本身输出 Perl 的版本。 这可能吗? 在 Perl 脚本中解析“perl --version”的输出似乎
人们还经常问“我怎样才能编译 Perl?”而他们真正想要的是创建一个可以在机器上运行的可执行文件,即使他们没有安装 Perl。 我知道有几种解决方案: perl2exe靛蓝之星 它是商业的。我从未尝试
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 8年前关闭。 Improve this
我是一名优秀的程序员,十分优秀!