Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Recreating hash

by kitkit201 (Initiate)
on Sep 12, 2012 at 01:50 UTC ( #993116=perlquestion: print w/ replies, xml ) Need Help??
kitkit201 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks, I'm trying to reverse engineer things.. First, i'm trying to make the following dump files to a sample hash so that I can extrapulate data on it. The hash is as folows.
-bash-3.2$ cat 1347395593.perl $VAR1 = { 'alerts' => [ { 'min_failure_count' => '3', 'status_history' => { 'status' => [ 1, 1, 1, 0, 0, 0, 1, 1, 0, 0 ], 'ts' => [ 1347395526, 1347395226, 1347394926, 1347394626, 1347394326, 1347394026, 1347393726, 1347393426, 1347393126, 1347392826 ] }, 'status_code' => 1, 'original_status_code' => 'BAD', 'dimensions' => { 'cluster' => '1', 'filesystem' => '/', 'hostgroup' => 'hiho', 'service' => 'system.fs-us +ed_pct', 'host' => 'limonata' }, 'best_group' => 'NO GROUP', 'timestamp' => 1347395226, 'text' => '95.019 (value) > 95 (max limit +) between Tue 20:19 - Tue 20:20 (UTC), 'check_type' => 'system.fs-used_pct', 'window_size' => '3' } ], 'bad_alerts' => 1, 'recovery_alerts' => 0 };
Can someone provide me a sample has on how to create it in the perl code and also how to extract data from the hash? I'm not quite understanding it. Thanks

Comment on Recreating hash
Download Code
Re: Recreating hash
by GrandFather (Cardinal) on Sep 12, 2012 at 01:59 UTC

    The data you show has an error in it. There should be a ' following "(UTC)".

    Assuming the code shown in the OP with the syntax error corrected:

    my %hash = %$VAR1; print "At $hash{alerts}[0]{timestamp}: $hash{alerts}[0]{text}\n";

    Prints:

    At 1347395226: 95.019 (value) > 95 (max limit) between Tue 20:19 - +Tue 20:20 (UTC)

    If that's not the help you wanted you'll need to be more explicit.

    True laziness is hard work
      Whops on the syntax error.. Thanks for your quick reply GF. I got the second part of my answer and now looking for the first part. Since this is the output of the hash, how would you recreate this hash from scratch in a perl program (with the same elements and what not)? I'm having trouble with the elements and the double references of => into =>.

        I don't understand "how to create it in the perl code" given that what you show is Perl code. What are you actually having trouble with? Maybe you should give us some context so we understand where you are coming from?

        True laziness is hard work
Re: Recreating hash
by NetWallah (Abbot) on Sep 12, 2012 at 05:29 UTC
    Here you go - Hash creation code:
    use strict; use warnings; my %hash =( 'alerts' => [ { 'min_failure_count' => '3', 'status_history' => { 'status' => [ 1, 1, 1, 0, 0, 0, 1, 1, 0, 0 ], 'ts' => [ 1347395526, 1347395226, 1347394926, 1347394626, 1347394326, 1347394026, 1347393726, 1347393426, 1347393126, 1347392826 ] }, 'status_code' => 1, 'original_status_code' => 'BAD', 'dimensions' => { 'cluster' => '1', 'filesystem' => '/', 'hostgroup' => 'hiho', 'service' => 'system.fs-us +ed_pct', 'host' => 'limonata' }, 'best_group' => 'NO GROUP', 'timestamp' => 1347395226, 'text' => '95.019 (value) > 95 (max limit +) between Tue 20:19 - Tue 20:2+0 (UTC)', 'check_type' => 'system.fs-used_pct', 'window_size' => '3' } ], 'bad_alerts' => 1, 'recovery_alerts' => 0 );
    My guess is that you were looking for the parens ().

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

Re: Recreating hash
by Kenosis (Priest) on Sep 12, 2012 at 06:59 UTC

    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!

Re: Recreating hash
by aitap (Deacon) on Sep 12, 2012 at 16:07 UTC

    Data::Dumper is used to create such dumps. For example,

    use Data::Dumper; print Dumper \%myhash;
    ("\" is useful (especially in case of arrays), because it creates a refecence, so Dumper dumps the structure as a single object).

    To get your structure back in the program, you can use do or eval. See Data::Dumper and eval for more info on restoring Dumper files.

    Sorry if my advice was wrong.
Re: Recreating hash
by kitkit201 (Initiate) on Sep 12, 2012 at 23:50 UTC
    Thanks for the help guys! That made a lot of sense I am slowly learning syntax for perl. So now my question has evolved to.. I want to compare array hash strings. Specifically I want to do something like this, where i am comparing the $hash{alerts}[0]{status_history}{status} string to a string of them. (The english translation is, I want to compare that status_history,status strings of 1's. If there are at least 6 ones in the front of the arry, do stuff)
    if ($hash{alerts}[0]{status_history}{status} =~ /1,1,1,1,1,1/) { do cool stuff }
    Is that correct>?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993116]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-10-24 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (134 votes), past polls