Spoon71 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use a Perl module (Opsview::REST) it wants to use a data structure that looks like below. I'm properly confused as to what this is hash of hashes or whatever...

'hostattributes' => [ { 'value' => 'some value +1', 'name' => 'URL', 'arg2' => undef, 'arg4' => undef, 'arg1' => 'someurl.com +', 'arg3' => undef }, { 'arg3' => undef, 'arg1' => 'someurl..co +m', 'name' => 'URL', 'arg4' => undef, 'arg2' => undef, 'value' => 'some value + 2' } ],

I need to create it programatically as I need to read and parse data to create multiple blocks (which is no problem) then once created I need to pass it into the Opsview::Rest piece as below

my $host = $ops->create_or_update_host( ip => 'nodename.com', name => "Name", hostgroup => { name => 'RDG - Prod Services' }, hosttemplates => [ { 'ref' => '/rest/config/hosttemplate/94 +', 'name' => 'AA-RDG-Pseudo-services' } ], hostattributes => PASS HERE, notification_period => { name => 'RDG-24x7-Minus-Bounce' } );

I'm an occasional Perl programmer so this may be simple to some but I'm bloody confused, any help welcome

Replies are listed 'Best First'.
Re: Data Structure confusion...
by talexb (Chancellor) on Apr 26, 2016 at 15:47 UTC

    Here's a quick primer on reading Perl data structures:

    • 'something' => (any data structure) - a hash key/value pair.
    • [ (a list of things) ] - an arrayref,
    • { key => value, .. } - a hashref
    • ( key => value, .. ) - a hash
    So, using those rules, your 'hostattributes' key has a value of an arrayref that contains two elements, each of which is a hashref, each of which contains five key/value pairs. Because this is a hash, the keys can appear in any order.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: Data Structure confusion...
by AnomalousMonk (Archbishop) on Apr 26, 2016 at 13:52 UTC

    Not specifically responsive to your question, but see Perl Data Structures Cookbook for general info on this topic.


    Give a man a fish:  <%-{-{-{-<

Re: Data Structure confusion...
by stevieb (Canon) on Apr 26, 2016 at 13:49 UTC

    Welcome to the Monastery, Spoon71!

    What you have there is an array of hashes (AoH), contained within an existing hash, referenced by a hostattributes key.

    To help you create this structure, we'll need a small sample of your input data.

Re: Data Structure confusion...
by NetWallah (Canon) on Apr 26, 2016 at 16:55 UTC
    I would suggest building the required structure this way:
    my @hostatt; push @hostatt, # Info for First host { 'value' => 'some value1', 'name' => 'URL', 'arg2' => undef, 'arg4' => undef, 'arg1' => 'someurl.com', 'arg3' => undef }; push @hostatt, # Info for Second host { 'value' => 'some value2', 'name' => 'URL2', 'arg2' => undef, 'arg4' => undef, 'arg1' => 'someur2.com', 'arg3' => undef }; #Finally, assign this to the main structure my $host = $ops->create_or_update_host( ip => 'nodename.com', name => "Name", hostgroup => { name => 'RDG - Prod Services' }, hosttemplates => [ { 'ref' => '/rest/config/hosttemplate/94 +', 'name' => 'AA-RDG-Pseudo-services' } ] +, hostattributes => \@hostatt, # <<< HERE <<<<< notification_period => { name => 'RDG-24x7-Minus-Bounce' } );

            This is not an optical illusion, it just looks like one.

      Perfect this works!!!! Thanks