Hi all,

I'm currently working on a script that basically needs to read data from one of our databases, do some fairly basic processing on it, and then output that data to a file for reporting.

The data is returned from the database in an AOH, where each hash of the array corresponds to a row returned from the table.

Once this data has been read, the script takes each hash in turn, reads the customer ID, and finds the list of open orders for that customer. It then attempts to create a copy of the original hash for each order id, and pushes each new hash onto an output array.

The problem seems to occur with the push. If I output the data hash-by-hash just prior to the push, I get perfect data, but if I dump out the final array once the loop is complete, I seem to get very corrupt data - a whole load of undef and references to $output->[1].

The loop I'm using to modify the data and push onto the new array is as follows:

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; print Dumper $hash; # outputting here yields perfect data push @$output, $hash; } } print Dumper $output; # outputting here gives trashed data
I've been trying to figure this out for ages, and I've not had any luck - I'm wondering if I have referencing problems, but I've tried modifying the push with no luck as yet.

Can anyone see where this is going wrong? Any suggestions at this point would be appreciated :)

Thanks in advance,
-- Foxcub
A friend is someone who can see straight through you, yet still enjoy the view. (Anon)


In reply to Push corrupting data by Tanalis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.