-1

Thanks in advance and your answer is most helpful.

I am fetching two arrayofhashref using $sth->fetchall_arrayref({}) from Database let's a and b where

a= [
          {
            'name' => 'test',
            'id' => '10',`enter code here`
          },
          {
            'name' => 'foo',
            'id' => '22',
          }
   ];

b= [
      {
            'dept' => 'IT',
            'mob' => '880978'
          },
          {
            'dept' => 'CSE',
            'mob' => '877687'
          },
       ];

The o/p should be :


a= [
          {
            'name' => 'test',
            'id' => '10',
             b= [
                   {
                     'dept' => 'IT',
                     'mob' => '880978'
                   },
                  {
                    'dept' => 'CSE',
                    'mob' => '877687'
                 },
              ];
          },
          {
            'name' => 'foo',
            'id' => '22',
             b= [
                  {
                   'dept' => 'IT',
                   'mob' => '880978'
                 },
                {
                   'dept' => 'CSE',
                   'mob' => '877687'
                 },
              ];

          }
   ];

I did like :
my $count = 0;
foreach my $row (@$a){
     $a->[$count]{b} = $b;
    $count++;
}

If I pass $b as "heloo" it is working fine , but how can I pass this array of hash ref ?

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50

1 Answers1

-1

I am not sure if I uderstood your question, but your code seems to be working.

#!/usr/bin/env perl

use strict;
use warnings;

use Test::More tests => 1;

my $a = [
    {
        'name' => 'test',
        'id' => '10',
    },
    {
        'name' => 'foo',
        'id' => '22',
    }
];

my $b = [
    {
        'dept' => 'IT',
        'mob' => '880978'
    },
    {
        'dept' => 'CSE',
        'mob' => '877687'
    },
];

#The o/p should be :

my $wanted = [
    {
        'name' => 'test',
        'id'   => '10',
        b      => [
            {
                'dept' => 'IT',
                'mob' => '880978'
            },
            {
                'dept' => 'CSE',
                'mob' => '877687'
            },
        ],
    },
    {
        'name' => 'foo',
        'id'   => '22',
        b      => [
            {
                'dept' => 'IT',
                'mob' => '880978'
            },
            {
                'dept' => 'CSE',
                'mob' => '877687'
            },
        ],
    }
];

#I did like :

my $count = 0;
for my $row (@$a){
    $a->[$count]{'b'} = $b;
    $count++;
}

is_deeply($a, $wanted, 'data structures equal');

gives:

1..1
ok 1 - data structures equal
JMa
  • 66
  • 6
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/13672049) – Comintern Sep 22 '16 at 00:17
  • I have maybe misunderstood the question, but why do you think my answer is wrong? I tried to explain that author´s code already does what (s)he is asking for. – JMa Sep 23 '16 at 12:46