http://www.perlmonks.org?node_id=1022030

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Monks!
I need to add the values from $listA to @listB, I am later sending @listB as json to another program. What is the best way of doing this? Here is a sample code to show what I am trying to do:
... use Data::Dumper; ... my $listA; $VAR1 = [ { 'ZIP' => '01111', 'CITY' => 'New York, NY', 'STREET_NAME_1' => 'CONNECTOR PARK', 'STREET_NAME_2' => '100 MAINST' } ]; my @listB; $VAR1 = { 'account' => '73240', 'name' => 'G Church CO' }; $VAR2 = { 'account' => '73240', 'name' => 'A Church CO' }; $VAR3 = { 'account' => '73240', 'name' => 'B Church CO' }; $VAR4 = { 'account' => '73240', 'name' => 'C Church CO' }; $VAR5 = { 'account' => '73240', 'name' => 'D Church CO' }; $VAR6 = { 'account' => '73240', 'name' => 'E Church CO' };
Thanks for the help!100 MAINSTCONNECTOR PARK

Replies are listed 'Best First'.
Re: Combining data help!
by kennethk (Abbot) on Mar 06, 2013 at 16:54 UTC
    What do you mean by "the values from $listA to @listB"? Do you mean append them to the end of the list (push)? Do you mean insert those fields into the hash refs? See How do I post a question effectively?. In particular, showing code and desired output would be very helpful.

    Assuming you are trying to insert the key-value pairs in $listA into each element of @listB, using Foreach Loops seems pretty straight forward:

    foreach my $acct (@listB) { %$acct = (%$acct, %{$listA->[0]}); }

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Sorry for the incosistency, but just need to add this block
      { 'ZIP' => '01111', 'CITY' => 'New York, NY', 'STREET_NAME_1' => 'CONNECTOR PARK', 'STREET_NAME_2' => '100 MAINST' } ]
      to the other variable.
Re: Combining data help!
by CountZero (Bishop) on Mar 06, 2013 at 16:52 UTC
    What have you tried?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Combining data help!
by tmharish (Friar) on Mar 06, 2013 at 16:58 UTC
      What about this:
      push @listB, @$listA;

        Of course!

        I just focused on the JSON creation - The array merge I thought the OP should figure out ...