【发布时间】:2020-08-27 12:01:22
【问题描述】:
我有许多错误字符串。我将它们与我已经拥有的模式相匹配。如果它们完全相同,我希望它们属于完全相同的错误失败。如果它们与模式匹配但与哈希中的先前字符串有一些不同,我想给它相同的错误名称,但附加一个不同的数字。
这是一个示例输入文件:
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'
};
对此的任何指导都会非常有帮助。还有很多我想补充的,但我对如何开始这样做感到很困惑。这甚至可以做到吗?
【问题讨论】:
-
您是否考虑过拥有一个键
syntax,其值是一个包含所有此类消息的数组引用,以及一个带有数组引用的键format?等等。 -
@zdim 每次消息匹配不同的模式时,我如何获得它来创建一个新密钥?
-
但是——所有这些从何而来?也许您可以更好地组织该过程,以便将它们全部整理出来。有很多很棒的模块可以记录各种消息、错误等等。
-
向我提供了包含所有错误的日志文件。但是,对于每个键,正如我所提到的,如果模式不同,我希望它自动创建一个带有硬编码名称的新存储桶名称,但在末尾附加一个 +1 数字。这可能吗?
-
发布了一个答案,但我想得到一些澄清,以使其更好。如答案中所述,主要是关于您期望什么样的错误消息
标签: perl