The problem is that $sortedCollectionData only contains the indices into @collectionData, but you try to access it as if it contains the elements themselves when you try to print the data:

... <th class=graycenter>$sortedCollectionData[$i]{'CollectionId'}</th> ...

should be

<th class=graycenter>$collectionData[$sortedCollectionData[$i]]{'Colle +ctionId'}</th>

The deeper problem is, that your subroutine does too many things at once, which makes debugging such stuff much harder than it needs to be. I split up the subroutine into three steps, readCollectionData, sortCollectionData (where I thought the problem was) and printCollectionData (where I found the problem). That made it much easier to separate the things and see what each step returns as results.

my $orderby = 'collection'; my @data = readCollectionData('dummyFilename.txt'); my @indices = sortCollectionData($orderby, @data); print Dumper \@indices; printCollectionData(\@data, @indices);

As an aside, you had relatively large if ... elsif ... else ... blocks that decide on what to sort and the same kind again to translate the short status code into a long status message. I replaced them by a hash lookup:

... if($sortedCollectionData[$i]{'Status'} eq "I") { $longStatus = "Incomplete"; }elsif($sortedCollectionData[$i]{'Status'} eq "SI") { $longStatus = "Submitted Incomplete"; }elsif($sortedCollectionData[$i]{'Status'} eq "C") { $longStatus = "Submitted"; }else{ $longStatus = "Submitted Complete"; } ...

becomes

my %translateLongStatus = ( 'I' => 'Incomplete', 'SI' => 'Submitted Incomplete', 'C' => 'Submitted', ); ... my ($longStatus, $rowStyle, $rowColor); $longStatus = $translateLongStatus{ $collectionData[$i]{Status} } +||'Submitted Complete'; ... }

In the end, my program looks like this (with much of the HTML printing removed):

#!perl -w use strict; use Data::Dumper; sub readCollectionData { my $arcFile = shift; my (@collectionData); my $semaphore = $arcFile . '.lock'; my ($longStatus, $rowStyle, $rowColor); #open(LOCKFILE, ">>$semaphore") or die "$semaphore: $!"; #flock(LOCKFILE, LOCK_EX) or die "flock() failed for $semaphore: $!" +; #open (ARCFILE, "<$arcFile") or die "Failed to open $arcFile: $!"; local *ARCFILE = *DATA; # Retrieve file information from arcFile as an array of hashes while( <ARCFILE> ) { my( $col, $cnt, $stat, $miss, $mod) = m[ ^ CollectionId \s* => \s* (\d+)? \s* Framecount \s* => \s* (\d+)? \s* Status \s* => \s* (\w+)? \s* Missing \s* => \s* ([\d,]+)? \s* Modified \s* => \s* ([\d/]+\s[\d:]+)? \s* $ ]x or warn "Bad format at line $.\n" and next; my( $modday, $modmon, $modyear, $modhrs, $modmin, $modsec ) = $mod =~ m[(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)] or warn "Bad date format in line $." and next; push @collectionData, { CollectionId => $col, Framecount => $cnt, Status => $stat, Missing => $miss, Modified => sprintf( "%4d/%02d/%02d %02d:%02d:%02d", $modyear, $modmon, $modday, $modhrs, $modmin, $modsec ), }; } return @collectionData }; # Map the program orderby names to the internal names in the hash my %sort_columns = ( collection => 'CollectionId', framecount => 'Framecount', status => 'Status', missing => 'Missing', ); sub sortCollectionData { my ($orderby, @collectionData) = @_; # Sort the collection according to orderBy param my $sort_col = $sort_columns{ $orderby } || 'Modified'; my @sortedCollectionData = sort { $collectionData[ $b ]{$sort_col} <=> $collectionData[ $a ]{$sort_c +ol} || $collectionData[ $b ]{Modified} cmp $collectionData[ $a ]{Modified +} } 0 .. $#collectionData; return @sortedCollectionData } my %translateLongStatus = ( 'I' => 'Incomplete', 'SI' => 'Submitted Incomplete', 'C' => 'Submitted', ); sub printCollectionData { my ($collectionData, @sortedCollectionData) = @_; my @collectionData = @$collectionData; my $transactions = scalar(@collectionData); for (my $i=0; $i < $transactions; $i++) { my ($longStatus, $rowStyle, $rowColor); $longStatus = $translateLongStatus{ $collectionData[$i]{Status} } +||'Submitted Complete'; if (($i%2) == 0){ $rowStyle = "oddrow"; $rowColor = "#e5e5e5"; } else { $rowStyle = "evenrow"; $rowColor = "#ffffff"; } print $i, $longStatus, $collectionData[ $i ]->{CollectionId}, "\n" +; } } my $orderby = 'collection'; my @data = readCollectionData('dummyFilename.txt'); my @indices = sortCollectionData($orderby, @data); print Dumper \@indices; printCollectionData(\@data, @indices); __DATA__ CollectionId=>26154 Framecount=>6 Status=>SC Missing=>0 Modified=>01/2 +2/2012 22:12:09 CollectionId=>26155 Framecount=>6 Status=>I Missing=>4 Modified=>01/22 +/2012 22:12:20 CollectionId=>25000 Framecount=>6 Status=>SC Missing=>0 Modified=>01/2 +2/2012 22:13:07 CollectionId=>25002 Framecount=>6 Status=>I Missing=>5 Modified=>01/22 +/2012 22:13:14 CollectionId=>25009 Framecount=>6 Status=>I Missing=>5 Modified=>01/22 +/2012 22:13:19 CollectionId=>25309 Framecount=>6 Status=>I Missing=>5 Modified=>01/22 +/2012 22:13:25 CollectionId=>25349 Framecount=>6 Status=>I Missing=>5 Modified=>01/22 +/2012 22:13:31 CollectionId=>25318 Framecount=>6 Status=>I Missing=>5 Modified=>01/22 +/2012 22:13:37 CollectionId=>21318 Framecount=>6 Status=>I Missing=>5 Modified=>01/22 +/2012 22:13:43 CollectionId=>21342 Framecount=>6 Status=>I Missing=>5 Modified=>01/22 +/2012 22:13:56

In reply to Re: Sorting an array or hashes by Corion
in thread Sorting an array or hashes by hok_si_la

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.