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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |