in reply to Recreating hash
NetWallah showed how to recreate the hash, and GrandFather demonstrated "...how to extract data from the hash...:"
print "At $hash{alerts}[0]{timestamp}: $hash{alerts}[0]{text}\n"; ^ ^ ^ | | | | | + - hash key | + - zeroth element of array + - hash key # {} means hash; [] means array
If you add and run the following line below the initialized hash:
say "$_ => $hash{$_}" for keys %hash;
it'll generate the following output:
alerts => ARRAY(0x2562880) recovery_alerts => 0 bad_alerts => 1
The key recovery_alerts is associated with the value 0, and the key bad_alerts is associated with the value 1. However, the key alerts is associated with an array reference, thus the 'alerts' => [... notation above in the hash initialization.
So, what's in that array--or at least the zeroth element? Try the following (the [0] notation dereferences the array reference, specifying element 0):
print $hash{alerts}[0];
Output:
HASH(0x379350)
It's a hash reference, and the hash begins here in the hash initialization:
'alerts' => [ { ^ | + - beginning of hash
We can print the keys of this hash with the following:
say "$_ => $hash{alerts}[0]{$_}" for keys %{$hash{alerts}[0]};
Output:
min_failure_count => 3 status_history => HASH(0x4ec628) status_code => 1 original_status_code => BAD dimensions => HASH(0x4f9440) best_group => NO GROUP timestamp => 1347395226 text => 95.019 (value) > 95 (max limit) between Tue 20:19 - Tue 20: +20 (UTC) window_size => 3 check_type => system.fs-used_pct
Remember that $hash{alerts}[0] contains a hash reference. We enclose this with %{} to dereference it to get its keys in the line above. Each key (the values taken by $_) from the hash is 'plugged' into $hash{alerts}[0]{$_}, to get the key's associated value.
Does the structure $hash{alerts}[0]{$_} look familiar? Look back to the line GrandFather created:
print "At $hash{alerts}[0]{timestamp}: $hash{alerts}[0]{text}\n"; ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
From the structure you provided, he showed "...how to extract data from the hash..."
Hope this helps!
|
|---|