简体   繁体   English

perl关于hash和数组的代码解释

[英]perl code interpretation regarding hash and array

I'm not familiar about perl but need to interpret(start learning) perl codes.我对 perl 不熟悉,但需要解释(开始学习)perl 代码。

Below code block is the part of the long code.下面的代码块是长代码的一部分。

my @blacklist;
open my $fh, '<', '/var/cache/nagios3/stato/json_full/SStatus.json';
close($fh);

my @json = decode_json(<$fh>);

push @blacklist, 1212, 2341, 3121, 5462;

my %outputs;
$outputs{$_} = [] for @blacklist;

foreach (sort { $a->{host_posting} cmp $b->{host_posting} } values %{$json[0]}) {
  1. Can you explain the meaning of $outputs{$_} = [] for @blacklist;你能解释一下$outputs{$_} = [] for @blacklist; ? ?
  2. Can you interpret of the last line?你能解释最后一行吗? -- In json file, there are keys named "host_posting". -- 在 json 文件中,有名为“host_posting”的键。 (It's for loop but cannot understand the meaning of $a->{host_posting} and $b->{host_posting} ) (是for循环但看不懂$a->{host_posting}$b->{host_posting}的意思)

Also, is there any effective way to search this kind of syntax things from stackoverflow or google?另外,有没有什么有效的方法可以从 stackoverflow 或 google 搜索这种语法?

Honestly, if you're just starting with Perl there are some rather complex concepts at work here.老实说,如果您只是从 Perl 开始,这里有一些相当复杂的概念在起作用。 I wouldn't expect someone new to Perl to deal with array and hash references until they had mastered the basics of the language.在掌握该语言的基础知识之前,我不会指望 Perl 的新手来处理数组和 hash 引用。

 my %outputs;

This declares a hash called %outputs .这声明了一个名为%outputs的 hash 。

 $outputs{$_} = [] for @blacklist;

This is a shortened way to write:这是一种缩短的编写方式:

for my $item (@blacklist) {
  $outputs{$item} = [];
}

There are three pieces of syntax here.这里有三段语法。

for my VAR (LIST) {
  ...
}

This goes through all of the elements in a LIST one at a time.这一次遍历 LIST 中的所有元素。 Each of the elements, in turn, is put into VAR and the block of code is executed.依次将每个元素放入 VAR 并执行代码块。 You can omit the VAR, in which case Perl uses $_ instead.您可以省略 VAR,在这种情况下 Perl 使用$_代替。

$outputs{$_} = ...;

This sets the value associated with a key in a hash called %outputs .这将设置与 hash 中称为%outputs的键关联的值。 The value $_ is going to be one of the elements from @blacklist .$_将是来自@blacklist的元素之一。

[];

This creates an empty array and returns a reference to it.这将创建一个空数组并返回对它的引用。

So, taking those pieces and putting them together, your code creates a key/value pair in your %outputs hash for each of the elements in @blacklist .因此,将这些部分放在一起,您的代码会在%outputs hash 中为@blacklist中的每个元素创建一个键/值对。 The key is the element from @blacklist and the value is a reference to an empty array.键是来自@blacklist的元素,值是对空数组的引用。

 foreach (sort { $a->{host_posting} cmp $b->{host_posting} } values %{$json[0]}) {

This is the start of a foreach loop.这是foreach循环的开始。 This is the same as the for loop that we saw before.这与我们之前看到的for循环相同。

$json[0]

This gets the first element from an array called @json .这从名为@json的数组中获取第一个元素。

%{$json[0]}

This assumes that the value stored in $json[0] is a reference to a hash.这假设存储在$json[0]中的值是对 hash 的引用。 It then dereferences that reference to get back to the original hash.然后它取消引用该引用以返回原始 hash。

values %{$json[0]}

This gets a list of the values from that hash.这会从 hash 中获取值列表。

sort { $a->{host_posting} cmp $b->{host_posting} } values %{$json[0]}

This gets a sorted version of that list of values.这将获得该值列表的排序版本。

A call to sort looks like this: sort调用如下所示:

sort { SORTING_CODE } LIST

The input list is your list of values that you got from the hash.输入列表是您从 hash 获得的值列表。 The sorting code is this:排序代码是这样的:

$a->{host_posting} cmp $b->{host_posting}

I don't have time to explain how sorting code works (see the documentation ) but this is assuming that each of the values in your list is a hash reference and it's sorting by the host_posting value within those hashes.我没有时间解释排序代码的工作原理(请参阅文档),但这是假设列表中的每个值都是 hash 引用,并且它是按这些哈希值中的host_posting值排序的。 The -> is (in this case) the way to get from a hash reference to a value associated with a key - $hash_ref->{key} . ->是(在这种情况下)从 hash 引用获取与键关联的值的方式 - $hash_ref->{key}

So, putting this code together, it gets the values (which are hash references) from the hash that is inside your JSON.因此,将这段代码放在一起,它会从 JSON 中的 hash 中获取值(即 hash 引用)。 It then sorts those values on the host_posting key and uses the resulting sorted list as the input for the foreach loop.然后它在host_posting键上对这些值进行排序,并使用生成的排序列表作为foreach循环的输入。 The loop puts each value in turn into the $_ variable and then executes the loop code block (which you haven't shown us).循环将每个值依次放入$_变量中,然后执行循环代码块(您尚未向我们展示)。

$outputs{$_} = [] for @blacklist;

all that's doing is defaulting all the items to an empty array.所做的只是将所有项目默认为一个空数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM