- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些错误字符串。我将它们与我已有的模式相匹配。如果它们完全相同,我希望它们属于完全相同的错误故障。如果它们匹配模式但与散列中的先前字符串有一些不同,我想给它相同的错误名称但附加不同的数字。
这是一个示例输入文件:
there are 5 syntax issues with semicolon
there are 11 syntax issues with semicolon
the file contains 5 formatting issues
there are 1 syntax issues with semicolon
check script for formatting issues
2 syntax issues have been found
the file contains 1 formatting issues
6 syntax issues have been found
use warnings;
use strict;
my %errors;
my $file = "listoferrormessages.txt"
open my $fh,'<',$file or die "Could not open $file: $!";
while(my $line = <$fh>){
if( $line =~ /syntax/){
if ($line =~ /there are \d syntax issues with semicolon/){
#if line matching format exists in hash values, continue
#if not, create a hash key called syntax_# where # increments one from the last key with the error name.
$errors{errorname} = $line;
}
elsif ($line =~ /\d syntax issues have been found/){
#same as above
$errors(errorname} = $line;
}
elsif ($line =~ /format/){
#same as above
}
}
close $fh;
我希望我的散列看起来像:
$VAR1 = {
'syntax_1' =>
'there are 5 syntax issues with semicolon',
'syntax_2' =>
'2 syntax issues have been found',
'format_1' =>
'the file contains 5 formatting issues',
'format_2' =>
'check script for formatting issues'
};
关于此的任何指导都会非常有帮助。还有很多我想补充的,但我对如何开始做这件事感到很困惑。这甚至可以做到吗?
最佳答案
这完成了所要求的,剩下的问题是可能的错误类型。
辅助数据结构 (%seen_error_type
) 用于避免搜索每一行的值,以检查是否已看到该错误类型;使用此散列,它只是一个查找。
use warnings;
use strict;
use feature qw(say);
use Data::Dump qw(dd); # to show complex data structures
my $file = shift // die "Usage: $0 file\n"; #/
open my $fh, '<', $file or die "Can't open $file: $!";
my (%error, %seen_error_type, $cnt_syntax, $cnt_format);
LINE:
while (my $line = <$fh>) {
chomp $line;
my $error_type = $line =~ s/[0-9]+/N/r; # extract error type
next LINE if exists $seen_error_type{$error_type};
$seen_error_type{$error_type} = 1;
if ($line =~ /syntax/) {
++$cnt_syntax;
$error{ "syntax_$cnt_syntax" } = $line;
}
elsif ($line =~ /format/) {
++$cnt_format;
$error{ "format_$cnt_format" } = $line;
}
else { } # specify how to handle unexpected error types
}
dd \%error;
错误“类型”首先从一行构建,方法是用N
替换数字;这仅遵循 OP 示例,因为没有给出如何对这些错误消息进行分类的规则。如果这确实是全部,那很好。但我预计会出现更复杂的错误类型标准。
改进这一点的关键需要是阐明预期“错误类型”(错误消息的结构)的规则。
简单地将意外模式添加到我们的错误类型簿记散列中是没有意义的,除非我们有一些关于如何从一行中提取模式的规则。否则,每一行可能的文本最终都可能成为其自身的关键,这将破坏对它们进行分类的整个练习的目的。
使用给定的输入文件,上面的打印
{ format_1 => "the file contains 5 formatting issues", format_2 => "check script for formatting issues", syntax_1 => "there are 5 syntax issues with semicolon", syntax_2 => "2 syntax issues have been found",}
(The Data::Dump module I used probably need be installed. A core option is Data::Dumper)
Another note, raised in comments: I don't see why to add a key for each new line, instead of adding each expected error-type-line to an arrayref for a suitable key (syntax
, format
, etc).
If there is no specific reason for that then I'd rather suggest something like
my (%error, %seen_error_type);
LINE:
while (my $line = <$fh>) {
chomp $line;
my $error_type = $line =~ s/[0-9]+/N/r; # extract error type
next LINE if exists $seen_error_type{$error_type};
$seen_error_type{$error_type} = 1;
if ($line =~ /syntax/) {
push @{$error{syntax}}, $line;
}
elsif ($line =~ /format/) {
push @{$error{format}}, $line;
}
else { } # specify how to handle unexpected error types
}
dd \%error;
现在我们只为键 syntax
和键 format
提供了一个数组引用。
这打印
{ format => [ "the file contains 5 formatting issues", "check script for formatting issues", ], syntax => [ "there are 5 syntax issues with semicolon", "2 syntax issues have been found", ],}
关于Perl 自动化散列填充和 key 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62417319/
我是一名优秀的程序员,十分优秀!