Ing. Branislav Gerzo [IBG], on Monday, January 10, 2005 at 13:03
(+0100) thinks about:

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

I am answering to myself:

writing own code should be complex, why not to use module for that?
try a look at:
http://search.cpan.org/~mneylon/Hash-Merge-0.07/Merge.pm
so solution should be:

use strict;
use warnings;
use Data::Dumper;

use Hash::Merge qw( merge );
my %ha = ( 'test1' => [
                        { 
                          'test1_a' => 'a',
                          'test1_b' => 'b',
                        },
                        {
                          'test1_a' => 'a',
                          'test1_b' => 'b',
                        }
                      ] 
          );

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

my %result = %{ merge( \%ha, \%hb ) };

print Dumper(\%result);  
__END__

$VAR1 = {
          'test1' => [
                       {
                         'test1_a' => 'a',
                         'test1_b' => 'b'
                       },
                       {
                         'test1_a' => 'a',
                         'test1_b' => 'b'
                       },
                       {
                         'test1_c' => 'c'
                       },
                       {
                         'test1_d' => 'd'
                       }
                     ]
        };

-- 

 ...m8s, cu l8r, Brano.

["I've got a very bad feeling about this." - Han Solo]



-- 
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