In your for (0..$#display) loop, you check $display[$#display][0] to see if it matches something. The problem is, you may have already spliced an element of the array away, and so the array is now smaller.

For example, you may have an array of 4 elements ($#display == 3). Then you find an element again, so you splice it out, so the array is now 3 elements. However, your loop still is "0..$#display" which was "0..3" at the time that perl evaluated it. So you get back to evaluating $display[3][0] - and perl needs to autovivify $display[3] in order to evaluate $display[3][0]. That is the problem.

Solution 1 is to add a "last" right after the splice. Once you've found the serial number, you're not going to find another one - the purpose of this loop is to keep things unique. So there can't be another one - just get out, and you won't evaluate past the end of the array anymore.

Solution 2 is to move to HoH's. In this world, I would have a structure like this:

%data = ( serial_number => { A => 0 or 1, B => 0 or 1, # etc. order_stamp => number }, # etc. );
Then, I'd have an order number outside the loop, and each time I touch a serial number, I'd also assign $data{$serial}{order_stamp} = ++$order. Then, to get the desired order, just do a reverse sort on the order_stamp:
@serial_order = reverse sort { $data{$a}{order_stamp} <=> $data{$b}{ +order_stamp} } keys %data;
But if you're using AoA's for space reasons, well, you'll have to live with scanning for your desired serial number (sounds slow to me ;->). Note that rather than having A, B, C, D as separate keys, you can just put them in a 'data => []' key to get them all quickly. Up to you.


In reply to Re: Populate and sort AoA by Tanktalus
in thread Populate and sort AoA by nedals

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.