Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Combining data help!

by Anonymous Monk
on Mar 06, 2013 at 16:30 UTC ( #1022030=perlquestion: print w/replies, xml ) Need Help??
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?


    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 ...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022030]
Approved by tmharish
[Corion]: Yay. Traditional finance situation averted. Bonds can be quoted in amounts (1_000_000 EUR) or per unit (1 unit). And a traditional error is to trade 2_000_000 piece when you meant to trade 2_000_000 EUR.
[Corion]: (one of my scripts simply catches high amounts and I phone people making that trade, ideally before the payment is due)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2017-03-29 11:26 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (347 votes). Check out past polls.