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!


In reply to Re: Recreating hash by Kenosis
in thread Recreating hash by kitkit201

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.