Home > Blockchain >  perl code interpretation regarding hash and array
perl code interpretation regarding hash and array

Time:08-16

I'm not familiar about perl but need to interpret(start learning) perl codes.

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; ?
  2. Can you interpret of the last line? -- In json file, there are keys named "host_posting". (It's for loop but cannot understand the meaning of $a->{host_posting} and $b->{host_posting})

Also, is there any effective way to search this kind of syntax things from stackoverflow or google?

CodePudding user response:

Honestly, if you're just starting with Perl there are some rather complex concepts at work here. I wouldn't expect someone new to Perl to deal with array and hash references until they had mastered the basics of the language.

my %outputs;

This declares a hash called %outputs.

$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. Each of the elements, in turn, is put into VAR and the block of code is executed. You can omit the VAR, in which case Perl uses $_ instead.

$outputs{$_} = ...;

This sets the value associated with a key in a hash called %outputs. The value $_ is going to be one of the elements from @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. The key is the element from @blacklist and the value is a reference to an empty array.

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

This is the start of a foreach loop. This is the same as the for loop that we saw before.

$json[0]

This gets the first element from an array called @json.

%{$json[0]}

This assumes that the value stored in $json[0] is a reference to a hash. It then dereferences that reference to get back to the original hash.

values %{$json[0]}

This gets a list of the values from that 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 { SORTING_CODE } LIST

The input list is your list of values that you got from the 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. The -> is (in this case) the way to get from a hash reference to a value associated with a key - $hash_ref->{key}.

So, putting this code together, it gets the values (which are hash references) from the hash that is inside your JSON. It then sorts those values on the host_posting key and uses the resulting sorted list as the input for the foreach loop. The loop puts each value in turn into the $_ variable and then executes the loop code block (which you haven't shown us).

CodePudding user response:

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

all that's doing is defaulting all the items to an empty array.

  • Related