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

Hi,

i´m just trying to add a value to a hash (array) but i´m not able to handle this.
Maybe somebody could help me.

Here my hash how it is:
$VAR1 = { 'Actions' => [ { 'ID' => '0001', 'Action' => [ { 'Type' => 'Registry', 'Method' => 'CreateKey', 'Key' => 'HKEY_LOCAL_MACHIN +E\\SOFTWARE' }, { 'Type' => 'Registry', 'Method' => 'CreateKey', 'Key' => 'HKEY_LOCAL_MACHIN +E\\SOFTWARE' }. ], 'Description' => 'All possible returns of C +reateKey.', 'Name' => 'CreateKey' } ] };
And here how it should look after i added "value".
$VAR1 = { 'Actions' => [ { 'ID' => '0001', 'Action' => [ { 'Value' => [ { 'Type' => 'REG_ +DWORD', 'content' => 'T +est1' }, { 'Type' => 'REG_ +DWORD', 'content' => 'T +est2' } ], 'Type' => 'Registry', 'Method' => 'CreateKey', 'Key' => 'HKEY_LOCAL_MACHIN +E\\SOFTWARE' }, { 'Value' => [ { 'Type' => 'REG_ +DWORD', 'content' => 'T +est1' }, { 'Type' => 'REG_ +DWORD', 'content' => 'T +est2' } ], 'Type' => 'Registry', 'Method' => 'CreateKey', 'Key' => 'HKEY_LOCAL_MACHIN +E\\SOFTWARE' }. ], 'Description' => 'All possible returns of C +reateKey.', 'Name' => 'CreateKey' } ] };
Thanks in advanced

Replies are listed 'Best First'.
Re: Add value to array hash
by CountZero (Bishop) on Oct 18, 2009 at 17:58 UTC
    use strict; use Data::Dumper; my $VAR1 = { 'Actions' => [ { 'ID' => '0001', 'Action' => [ { 'Type' => 'Registry', 'Method' => 'CreateKey', 'Key' => 'HKEY_LOCAL_MACHIN +E\\SOFTWARE' }, { 'Type' => 'Registry', 'Method' => 'CreateKey', 'Key' => 'HKEY_LOCAL_MACHIN +E\\SOFTWARE' }, ], 'Description' => 'All possible returns of C +reateKey.', 'Name' => 'CreateKey' } ] }; $VAR1->{'Actions'}[0]{'Action'}[0]{'Value'} = [ { 'Type' => 'REG_DWORD', 'content' => 'Test1', }, { 'Type' => 'REG_DWORD', 'content' => 'Test2', } ]; print Dumper(\$VAR1);
    Output:
    $VAR1 = \{ 'Actions' => [ { 'ID' => '0001', 'Action' => [ { 'Value' => [ { 'Type' => +'REG_DWORD', 'content' +=> 'Test1' }, { 'Type' => +'REG_DWORD', 'content' +=> 'T+est2' } ], 'Type' => 'Registry', 'Method' => 'CreateKey', 'Key' => 'HKEY_LOCAL_MACH +INE\\SOFTWARE' }, { 'Type' => 'Registry', 'Method' => 'CreateKey', 'Key' => 'HKEY_LOCAL_MACH +INE\\SOFTWARE' } ], 'Name' => 'CreateKey', 'Description' => 'All possible returns of + CreateKey.' } ] };

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Add value to array hash
by zwon (Abbot) on Oct 18, 2009 at 17:41 UTC
    for my $action ( @{ $VAR1->{Actions}[0]{Action} } ) { $action->{Value} = [ { 'Type' => 'REG_DWORD', 'content' => 'Test1', }, { 'Type' => 'REG_DWORD', 'content' => 'Test2', }, ]; }