title: Mapping two arrays of hashes to a new data structure
body: <p>I am trying to take two arrays of hashes, and convert them to a single data structure. This new structure will be hashref, where the value to each key is an array of hashes.</p>
  <p>Below are the two structures I'm converting - which are the result of selectall_arrayref with the slice parameter on two seperate queries:</p>
  <pre><code>$mappings = [
                  {
                      bill_id        =&gt; '100',
                      linked_bill_id =&gt; '1000',
                  },
                  {
                      bill_id        =&gt; '100',
                      linked_bill_id =&gt; '1001',
                  },
                  {
                      bill_id        =&gt; '200',
                      linked_bill_id =&gt; '2000',
                  },
                  {
                      bill_id        =&gt; '200',
                      linked_bill_id =&gt; '2001',
                  },
                  {
                      bill_id        =&gt; '200',
                      linked_bill_id =&gt; '2002',
                  },
  ];
  
  $fees = [
                  {
                      bill_id     =&gt; '100',
                      payment_id  =&gt; '500',
                      version     =&gt; 1,
                      has_fee     =&gt; 0,
                  },
                  {
                      bill_id     =&gt; '100',
                      payment_id  =&gt; '501',
                      version     =&gt; 2,
                      has_fee     =&gt; 1,
                  },
                  {
                      bill_id     =&gt; '1000',
                      payment_id  =&gt; '502',
                      version     =&gt; 1,
                      has_fee     =&gt; 0,
                  },
                  {
                      bill_id     =&gt; '1001',
                      payment_id  =&gt; '503',
                      version     =&gt; 1,
                      has_fee     =&gt; 0,
                  },
                  {
                      bill_id     =&gt; '200',
                      payment_id  =&gt; '504',
                      version     =&gt; 1,
                      has_fee     =&gt; 0,
                  },
                  {
                      bill_id     =&gt; '2000',
                      payment_id  =&gt; '505',
                      version     =&gt; 1,
                      has_fee     =&gt; 0,
                  },
                  {
                      bill_id     =&gt; '2001',
                      payment_id  =&gt; '506',
                      version     =&gt; 1,
                      has_fee     =&gt; 0,
                  },
                  {
                      bill_id     =&gt; '2002',
                      payment_id  =&gt; '507',
                      version     =&gt; 1,
                      has_fee     =&gt; 1,
                  },
  ];
  </code></pre>
  <p>Using both structures, I want to create a new structure where the key is the bill_id from <code>$mappings</code>, and the value is an array of fees for the bill_id and all of its linked_bill_ids. It should like:</p>
  <pre><code>$VAR1 = {
              '100' =&gt; [
                          {
                              'bill_id'    =&gt; '100',
                              'payment_id' =&gt; '500',
                              'version'    =&gt; 1,
                              'has_fee'    =&gt; 0,
                          },
                          {
                              'bill_id'    =&gt; '100',
                              'payment_id' =&gt; '501',
                              'version'    =&gt; 2,
                              'has_fee'    =&gt; 1,
                          },
                          {
                              'bill_id'    =&gt; '1000',
                              'payment_id' =&gt; '502',
                              'version'    =&gt; 1,
                              'has_fee'    =&gt; 0,
                          },
                          {
                              'bill_id'    =&gt; '1001',
                              'payment_id' =&gt; '503',
                              'version'    =&gt; 1,
                              'has_fee'    =&gt; 0,
                          },
              ],
              '200' =&gt; [
                          {
                              'bill_id'    =&gt; '200',
                              'payment_id' =&gt; '504',
                              'version'    =&gt; 1,
                              'has_fee'    =&gt; 0,
                          },
                          {
                              'bill_id'    =&gt; '2000',
                              'payment_id' =&gt; '505',
                              'version'    =&gt; 1,
                              'has_fee'    =&gt; 0,
                          },
                          {
                              'bill_id'    =&gt; '2001',
                              'payment_id' =&gt; '506',
                              'version'    =&gt; 1,
                              'has_fee'    =&gt; 0,
                          },
                          {
                              'bill_id'    =&gt; '2002',
                              'payment_id' =&gt; '507',
                              'version'    =&gt; 1,
                              'has_fee'    =&gt; 1,
                          },          
              ]
  };
  
  </code></pre>
  <p>My code is below, which gets me pretty close but not exactly what I want. Also, I'm convinced that there's a far better method for transforming these data structures than what I'm currently doing. Maybe this can be simplified somehow by chaining maps/greps together for both arrays at once?</p>
  <p>I'm looking for help on getting this correct, and advice on how to do it better.</p>
  <pre><code>my $bill_fees;
  for my $bill (@$mappings) {
      push @{ $bill_fees-&gt;{bill_fee}{$bill-&gt;{bill_id} },
        grep { $_-&gt;{bill_id} eq $bill-&gt;{bill_id} } @$fees;
  
      my @linked_bills = map  { $_-&gt;{linked_bill_id} }
                         grep { $_-&gt;{bill_id} eq $bill-&gt;{bill_id} }
                         @$mappings;
  
      for my $linked_bill (@linked_bills) {
          push @{ $bill_fees-&gt;{bill_fee}{$bill-&gt;{bill_id}} },
            grep { $_-&gt;{bill_id} eq $linked_bill } @$fees;
      }
  }
  </code></pre>
  <p>The method above is slightly incorrect. It gives me the expected structue I listed above, <strong>but</strong> for each bill_id key I also end up with:</p>
  <pre><code>use Data::Dumper;
  print Dumper($bill_fees-&gt;{bill_fee});
  
  
  '100' =&gt; [
             $VAR1-&gt;{'100'}[0],
             $VAR1-&gt;{'100'}[1],
             $VAR1-&gt;{'100'}[2],
             $VAR1-&gt;{'100'}[3],
  ],
  '200' =&gt; [
             $VAR1-&gt;{'200'}[0],
             $VAR1-&gt;{'200'}[1],
             $VAR1-&gt;{'200'}[2],
             $VAR1-&gt;{'200'}[3],
             $VAR1-&gt;{'200'}[0],
             $VAR1-&gt;{'200'}[1],
             $VAR1-&gt;{'200'}[2],
             $VAR1-&gt;{'200'}[3],           
  ]
  </code></pre>
  <p>I assume this is because there are multiple of the same bill_ids in mappings. So, for example, <code>bill_id = 100</code> would get looped twice.</p>