2016-05-27 16 views
0

ありがとうございました。あなたの答えが最も役立ちます。別のキーをperlのハッシュリファレンスに追加することができません

私は「heloo」は、それが正常に動作しているが、どのように私はハッシュリファレンスのこの配列を渡すことができるよう$ Bを渡すと、私は

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++; 
} 

データベースLETのAとBとから$sth->fetchall_arrayref({})を使用して2つのarrayofhashrefをフェッチするのですか?

答えて

0

あなたの質問に納得がいくかどうかはわかりませんが、あなたのコードは動作しているようです。

#!/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'); 

ができます:

1..1 
ok 1 - data structures equal 
+0

これは、質問への答えを提供していません。十分な[評判](http://stackoverflow.com/help/whats-reputation)があれば、[任意の投稿にコメントする]ことができます(http://stackoverflow.com/help/privileges/comment)。代わりに、[質問者からの説明を必要としない回答を提供する](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- i-do-代わりに)。 - [レビューの投稿](レビュー/低品質の投稿/ 13672049) – Comintern

+0

質問が誤解されている可能性がありますが、なぜ私の答えが間違っていると思いますか?私は、著者のコードがすでに何を求めているのかを説明しようとしました。 – JMa

関連する問題