I'm populating, for display, an AoA with a unordered list of serial numbers. I need to group by prefix putting the last read group at the top of the list. The 'X' indicates the prefix read. "num" in the first column is there to display mysteriously added elements.

Display based on __DATA__ SNO A B C D num: 1011 X X X X num: 0001 X X X X num: 0011 X X X X num: 0201 X X X X num: num: ...

Based on the data given, I should get just the 4 items as shown, but for some reason, each time I attempt to update the AoA, an empty cell gets added. I've included a couple of print Dumper() lines where the problem occurs, but the reason it's occuring eludes me.

Can someone explain what's going on. (and if you know a better way to do this, I'm all ears\eyes)

use strict; use Data::Dumper; my @display = (); # AoA while (<DATA>) { build_display($_); } print " SNO A B C D\n"; for (@display) { print 'num: ' . join(' ', @$_) . "\n"; } print "\n"; ########################## sub build_display { ########################## my $serialno = shift; chomp $serialno; if ($serialno) { my ($prefix,$number) = ($serialno =~ /^(\w)(\w{4})/); my $j = ($prefix =~ /A/i) ? 1 : ($prefix =~ /B/i) ? 2 : ($prefix =~ /C/i) ? 3 : ($prefix =~ /D/i) ? 4 : 0; my $temp = ''; for (0..$#display) { if ($display[$_][0] =~ /^$number$/i) { ## Need to update +array_ref $temp = $display[$_]; ## copy to temp splice(@display, $_, 1); ## delete from array. Added +to top later. print "After Delete: " . Dumper(\@display); <stdin>; } } ## ?? Adds an element to the @display array if update is required. But + why OR how ?? print "Before Add: " . Dumper(\@display); <stdin>; ## if 'number' not in array, create a new entry. $temp = [$number,' ',' ',' ',' '] if (!$temp); $temp->[$j] = 'X'; ## flag the applicable prefix unshift @display, $temp; ## add to top of array. } } __DATA__ A0011 B0001 C0201 D0001 A1011 B0201 C1011 D0201 A0201 B0011 C0011 D0011 A0001 B1011 C0001 D1011

In reply to 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.