帮助理解 perl hash
这里是 Perl 新手...我在这个工作 perl 脚本上得到了一些 HASH 代码的帮助,我只需要帮助理解该代码,以及是否可以以一种我可以更轻松或直观地理解 HASHES 的使用的方式编写它?
总之,脚本执行正则表达式来过滤日期,正则表达式的其余部分将提取与该日期相关的数据。
use strict;
use warnings;
use constant debug => 0;
my $mon = 'Jul';
my $day = 28;
my $year = 2010;
my %items = ();
while (my $line = <>)
{
chomp $line;
print "Line: $line\n" if debug;
if ($line =~ m/(.* $mon $day) \d{2}:\d{2}:\d{2} $year: ([a-zA-Z0-9._]*):.*/)
{
print "### Scan\n" if debug;
my $date = $1;
my $set = $2;
print "$date ($set): " if debug;
$items{$set}->{'a-logdate'} = $date;
$items{$set}->{'a-dataset'} = $set;
if ($line =~ m/(ERROR|backup-date|backup-size|backup-time|backup-status)[:=](.+)/)
{
my $key = $1;
my $val = $2;
$items{$set}->{$key} = $val;
print "$key=$val\n" if debug;
}
}
}
print "### Verify\n";
for my $set (sort keys %items)
{
print "Set: $set\n";
my %info = %{$items{$set}};
for my $key (sort keys %info)
{
printf "%s=%s;", $key, $info{$key};
}
print "\n";
}
我想要理解的是这些行:
$items{$set}->{'a-logdate'} = $date;
$items{$set}->{'a-dataset'} = $set;
再往下几行:
$items{$set}->{$key} = $val;
这是哈希引用的示例吗?哈希值的哈希值?
我想我对 {$set} 的使用感到困惑:-(
Perl newbie here...I had help with this working perl script with some HASH code and I just need help understanding that code and if it could be written in a way that I would understand the use of HASHES more easily or visually??
In summary the script does a regex to filter on date and the rest of the regex will pull data related to that date.
use strict;
use warnings;
use constant debug => 0;
my $mon = 'Jul';
my $day = 28;
my $year = 2010;
my %items = ();
while (my $line = <>)
{
chomp $line;
print "Line: $line\n" if debug;
if ($line =~ m/(.* $mon $day) \d{2}:\d{2}:\d{2} $year: ([a-zA-Z0-9._]*):.*/)
{
print "### Scan\n" if debug;
my $date = $1;
my $set = $2;
print "$date ($set): " if debug;
$items{$set}->{'a-logdate'} = $date;
$items{$set}->{'a-dataset'} = $set;
if ($line =~ m/(ERROR|backup-date|backup-size|backup-time|backup-status)[:=](.+)/)
{
my $key = $1;
my $val = $2;
$items{$set}->{$key} = $val;
print "$key=$val\n" if debug;
}
}
}
print "### Verify\n";
for my $set (sort keys %items)
{
print "Set: $set\n";
my %info = %{$items{$set}};
for my $key (sort keys %info)
{
printf "%s=%s;", $key, $info{$key};
}
print "\n";
}
What I am trying to understand is these lines:
$items{$set}->{'a-logdate'} = $date;
$items{$set}->{'a-dataset'} = $set;
And again couple lines down:
$items{$set}->{$key} = $val;
Is this an example of hash reference? hash of hashes?
I guess i'm confused with the use of {$set} :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
%items
是哈希引用的哈希(从概念上讲,哈希的哈希)。$set
是%items
的键,然后你会得到另一个哈希值,它被添加到键'a-logdate'
和'a-dataset'
。(根据评论修正)
%items
is a hash of hash references (conceptually, a hash of hashes).$set
is the key into%items
and then you get back another hash, which is being added to with keys'a-logdate'
and'a-dataset'
.(corrected based on comments)
Lou Franco 的答案很接近,但有一个小印刷错误 - 哈希引用的哈希值是
%items
,而不是$items
。当您从%items
检索值时,它被称为$items{key}
,因为您检索的值是标量(在本例中为哈希引用) ),但$items
将是一个不同的变量。Lou Franco's answer is close, with one minor typographical error—the hash of hash references is
%items
, not$items
. It is referred to as$items{key}
when you are retrieving a value from%items
because the value you are retrieving is a scalar (in this case, a hash reference), but$items
would be a different variable.