Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found