1

I have two array of hashes .Both have similar values in them but i want to create new key in the hash that will have the some values of second array of hash.

First Array:

[
    { area_code => 93,   name => 'Afghanistan',    code => 'AF', slno => 4554 },
    { area_code => 1684, name => 'American Samoa', code => 'AS', slno => 4557 },
];

Second Array:

[
    { city => "Berat",  country => "AS", id => 134368 },
    { city => "Durres", country => "AS", id => 138466 },
    { city => "Kabul",  country => "AF", id => 142462 },
];

Now in the first hash i have key code whose value is similar to the second hash key country .So i want to add a new key in the second array of hash which will be country_name.And the country_name value will be the value of first array of hash name.

So how can we do this please help me in this

2 Answers 2

4
use strict;
use warnings;

my $a1 = [
    { area_code => 93,   code => "AF", name => "Afghanistan",    slno => 4554 },
    { area_code => 1684, code => "AS", name => "American Samoa", slno => 4557 },
];

my $a2 = [
    { city => "Berat",  country => "AS", id => 134368 },
    { city => "Durres", country => "AS", id => 138466 },
    { city => "Kabul",  country => "AF", id => 142462 },
];

my %h = map { $_->{code} => $_ } @$a1;
for my $v (@$a2) {
    $v->{country_name} = $h{ $v->{country} }{name};
}
Sign up to request clarification or add additional context in comments.

3 Comments

Please comment when down voting as reason is not obvious.
Though I didn't downvote, one potential downside I see is the copying of entire @$a1 which we can limit to $_->{code} => $_->{name} other wise it was clear to me so you have my upvote.
@jaypal yes, that could be the reason.
3

This is a similar idea to @mpapec's, but, I think, a little cleaner.

use strict;
use warnings;

my @array1 = (
   { area_code => 93,   name => 'Afghanistan',    code => 'AF', slno => 4554 },
   { area_code => 1684, name => 'American Samoa', code => 'AS', slno => 4557 },
);

my @array2 = (
   { country => 'AS', city => 'Berat',  id => 134368 },
   { country => 'AS', city => 'Durres', id => 138466 },
   { country => 'AF', city => 'Kabul',  id => 142462 },
);

{
   my %names = map { $_->{code} => $_->{name} } @array1;
   $_->{country_name} = $names{ $_->{country} } for @array2;
}

use Data::Dump;
dd \@array2;

output

[
  {
    country      => 'AS',
    city         => 'Berat',
    id           => 134368,
    country_name => 'American Samoa',
  },
  {
    country      => 'AS',
    city         => 'Durres',
    id           => 138466,
    country_name => 'American Samoa',
  },
  {
    country      => 'AF',
    city         => 'Kabul',
    id           => 142462,
    country_name => 'Afghanistan',
  },
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.