Hi Chris,
  Please, check comments and codes below.
On Sun, Jul 8, 2012 at 6:19 AM, Chris Stinemetz <chrisstinem...@gmail.com>wrote:

> Thank you very much for your responses.
>
> I have another question.
>
> I would like to replace the second element from hash1 with the second key
> from  %hash2
>
>     I believe what you wanted replaced is the value of each keys in the
hash1.


> Where both of the firsts keys match in the two hashes. I shortened the
> sample data below but there will be a match for each instances of the keys
> in the complete data.
>
>
     Non of the hashes keys ( i.e hash1 and hash2 ) matches in this
example, so I changed some, for the purpose of this example.

   Moreover, I think  %hash1 and %hash2, should have been declared as
     $hash1 = { ..... } , $hash2 = { ...... }
     OR
    %hash1 = ( .... ), %hash2 = ( .... )


> Thanks in advance,
>
> Chris
>
> %hash1 = {
>           '371' => 2,
>           '33' => 2,
>           '524' => 14,
>           '812' => 54,
>           '955' => 5,
>           '68' => 2,
>           '831' => 34,
>         };
>
>
> %hash2 = {
>           '105' => {
>                      'Name1' => 1
>                    },
>           '473' => {
>                      'Name6' => 1
>                    },
>           '925' => {
>                      'Name5' => 1
>                    },
>           '840' => {
>                      'Name4' => 1
>                    },
>           '3' => {
>                    'Name1' => 1
>                  },
>           '390' => {
>                      'Name6' => 1
>                    },
>           '146' => {
>                      'Name1' => 1
>                    },
>           '111' => {
>                      'Name2' => 1
>                    },
>           '38' => {
>                     'Name2' => 1
>                   },
>           '356' => {
>                      'Name6' => 1
>                    },
>           '802' => {
>                      'Name4' => 1
>                    },
>           '4' => {
>                    'Name1' => 1
>                  },
>           '528' => {
>                      'Name6' => 1
>                    },
>           '164' => {
>                      'Name2' => 1
>                    },
>           '196' => {
>                      'Name1' => 1
>                    },
>           '807' => {
>                      'Name4' => 1
>                    },
>           '945' => {
>                      'Name5' => 1
>                    }
>         };
>

  The following code does want you want, I suppose:

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my $hash1 = {
    '371' => 2,
    '33'  => 2,
    '524' => 14,
    '812' => 54,
    '955' => 5,
    '68'  => 2,
    '831' => 34,
};

my $hash2 = {
    '371' => { 'Name1' => 1 },
    '33'  => { 'Name6' => 1 },
    '925' => { 'Name5' => 1 },
    '840' => { 'Name4' => 1 },
    '3'   => { 'Name1' => 1 },
    '390' => { 'Name6' => 1 },
    '146' => { 'Name1' => 1 },
    '111' => { 'Name2' => 1 },
    '38'  => { 'Name2' => 1 },
    '356' => { 'Name6' => 1 },
    '831' => { 'Name4' => 1 },
    '4'   => { 'Name1' => 1 },
    '528' => { 'Name6' => 1 },
    '164' => { 'Name2' => 1 },
    '196' => { 'Name1' => 1 },
    '807' => { 'Name4' => 1 },
    '955' => { 'Name5' => 1 }
};

my %new_hash = ();
foreach my $data1 ( keys %$hash1 ) {
    while ( my ( $key, $value ) = each %$hash2 ) {
        my ($new_value) = keys %$value;
        $new_hash{$key} = $new_value if $data1 == $key;
    }
}

print Dumper \%new_hash;

OUTPUT from Dumper module:

$VAR1 = {
          '371' => 'Name1',
          '33' => 'Name6',
          '955' => 'Name5',
          '831' => 'Name4'
        };

If I understand your question.
-- 
Tim

Reply via email to