[Israel.pm] Importing values from a 2D hash into another 2D hash

Meir Guttman meir at guttman.co.il
Mon Oct 29 11:08:47 PDT 2012


Dear Perlers,

For the last two hours, my head is spinning, but I cannot find how to do the
following:

I have two hashes, both 2D, the %master and another one from which I want to
copy values to the master.

The end result should have the same "rows" as in the original master, but
with the the tuples of the second hash added to the inner hash of these
rows.

I need to do this in a subroutine, so what I have are two hash-references.

I am assuming that the general structure should be like the following. What
I am missing is the innermost assignment in the "foreach" of the sub

 

use strict;

use warnings;

use use Data::Dumper;

 

my %master = (

  row_1 => {value => 10, size => 1000},

  row_2 => {value => 20, size => 2000},

  row_3 => {value => 30, size => 3000},

  row_4 => {value => 40, size => 4000},

);

 

my %added = (

  row_1 => {line => 'one',   level => 'High'},

  row_2 => {line => 'two',   level => 'Med'},

  row_4 => {line => 'four',  level => 'Low'},

  row_5 => {line => 'five',  level => 'Low'},

  row_6 => {line => 'six',   level => 'Med'},

  row_7 => {line => 'seven', level => 'High'},

);

 

combine_values(\%master, \%added);

 

Dump (\%master);

 

Exit(0);

 

sub combine_values {

  my $master_href = shift;

  my $added_href  = shift;

 

  foreach my $key (sort keys %{$master_href}){

    # Please, what comes here????

  }

}

 

__END__

 

Dumper should output:

 

$VAR1 = {

  'row_1' => {

    'value' => 10,

    'size' => 1000,

    'line' => 'one',

    'level' => 'High',

  }

  'row_2' => {

    'value' => 20,

    'size' => 2000,

    'line' => 'two',

    'level' => 'Med',

  }

  'row_3' => {

    'value' => 30,

    'size' => 3000,

  }

  'row_4' => {

    'value' => 40,

    'size' => 4000

    'line' => 'four',

    'level' => 'Low'    

  }

}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.perl.org.il/pipermail/perl/attachments/20121029/a69fbc41/attachment.htm 


More information about the Perl mailing list