Subject: complex data structure

Hi pals,

How can I add values to hash, when key is the same ? Example:

my %ha = ( 'test1' => [
                        { 
                          'test1_a' => 'a',
                          'test1_b' => 'b',
                        },
                        {
                          'test1_a' => 'a',
                          'test1_b' => 'b',
                        }
                      ] 
          );

my %hb = ( 'test1' => [   
                        {
                          'test1_c' => 'c'
                        },
                        {
                          'test1_d' => 'd'
                        }
                      ]
         );

%ha is initial and will _always_ have all the keys, which has %hb. Now
I want this structure as result:

%result = ( 'test1' => [
                        { 
                          'test1_a' => 'a',
                          'test1_b' => 'b'
                        },
                        {
                          'test1_a' => 'a',
                          'test1_b' => 'b'
                        },
                        {
                          'test1_c' => 'c'
                        },
                        {
                          'test1_d' => 'd'
                        }
                      ] 
          );

order in hash doesn't matter. Anyone ?
thanks.


Hope this helps ...

jwm

my %ha = ( 'test1' => [ { 'test1_a' => 'a', 'test1_b' => 'b', }, { 'test1_a'
=>
'aa', 'test1_b' => 'bb', } ] );
my %hb = ( 'test1' => [   { 'test1_c' => 'c' }, { 'test1_d' => 'd' } ]);
foreach (keys %ha){
    print "key = <$_>\n";
    foreach $d (@{$ha{$_}}) {
        foreach $array_hash (keys %{$d}) {
            print "d{$array_hash} = ${$d}{$array_hash}\n";
            }
        }
    }
foreach (keys %hb) {
    push @{$ha{$_}}, @{$hb{$_}};
    }
print "\nafter push\n";
foreach (keys %ha){
    print "key = <$_>\n";
    foreach $d (@{$ha{$_}}) {
        foreach $array_hash (keys %{$d}) {
            print "d{$array_hash} = ${$d}{$array_hash}\n";
            }
        }
    }

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to