in reply to Push corrupting data

you're pushing the same hash on the list, i think you want to make a copy yes?

my $data = [ { cust_id => 1 }, { cust_id => 3 }, ]; my $order_data = { 1 => { order_ids => '5,6' }, 3 => { order_ids => '7,8,9' }, }; my $output = []; foreach my $hash (@$data) { my $cust_id = $hash->{cust_id}; foreach my $id (split ',', $order_data->{$cust_id}->{order_ids}) { $hash->{order_id} = $id; push @$output, {%{$hash}}; } } use Data::Dumper; print Dumper $output; __END__ $VAR1 = [ { 'cust_id' => 1, 'order_id' => '5' }, { 'cust_id' => 1, 'order_id' => '6' }, { 'cust_id' => 3, 'order_id' => '7' }, { 'cust_id' => 3, 'order_id' => '8' }, { 'cust_id' => 3, 'order_id' => '9' } ];

Replies are listed 'Best First'.
Re: Re: Push corrupting data
by Tanalis (Curate) on Mar 13, 2003 at 09:25 UTC
    Thanks - that seems to have solved the problem. <PI'm kind of lost as to why a rereferenced dereference ({%{$hash}} over simply $hash) works, but pushing the original reference doesn't - would you mind trying to explain this?

    Thanks again for your help .. *smiles*
    -- Foxcub
    A friend is someone who can see straight through you, yet still enjoy the view. (Anon)

      As I know very well since yesterday, dereferencing a hashref like this actually makes a copy of it. So when you do {%{$hash}} you create a new hashref that points to a new copy of whatever data $hash pointed to. That's why it works :).

      CU
      Robartes-