Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

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

I asked a question early last week concerning information extracting from a specific file format (Extracting information from file to Hash) and BrowserUK was good enough to point me in the right direction, however I am having an issue sorting my AoH. The error I am getting when running a command line trace is, "Can't use string ("1") as a HASH ref while "strict refs" in use at getCollections.pl line 164, <ARCFILE> line 10." I can reference and print the unsorted keys just fine, however the sorted AoH is empty. For instance $collectionData[$i]{'Missing'} contains a value however $sortedCollectionData[$i]{'Missing'} is undef.

Here is my file format:
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
Here is my sub:
sub printCollectionData { my $arcFile = shift; my $orderBy = shift; my (@collectionData, @sortedCollectionData); 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: $!"; # 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 ), }; } # Sort the collection according to orderBy param if($orderBy eq "collection") { @sortedCollectionData = sort { $collectionData[ $b ]{CollectionId} <=> $collectionData[ $a ]{Coll +ectionId} || $collectionData[ $b ]{Modified} cmp $collectionData[ $a ]{Modified +} } 0 .. $#collectionData; }elsif($orderBy eq "framecount") { @sortedCollectionData = sort { $collectionData[ $b ]{Framecount} <=> $collectionData[ $a ]{Framec +ount} || $collectionData[ $b ]{Modified} cmp $collectionData[ $a ]{Modified +} } 0 .. $#collectionData; }elsif($orderBy eq "status") { @sortedCollectionData = sort { $collectionData[ $a ]{Status} cmp $collectionData[ $b ]{Status} || $collectionData[ $b ]{Modified} cmp $collectionData[ $a ]{Modified +} } 0 .. $#collectionData; }elsif($orderBy eq "missing") { @sortedCollectionData = sort { $collectionData[ $b ]{Missing} <=> $collectionData[ $a ]{Missing} || $collectionData[ $b ]{Modified} cmp $collectionData[ $a ]{Modified +} } 0 .. $#collectionData; }else { @sortedCollectionData = sort { $collectionData[ $b ]{Modified} cmp $collectionData[ $a ]{Modified +} } 0 .. $#collectionData; } my $transactions = scalar(@collectionData); for (my $i=0; $i < $transactions; $i++) { 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"; } if (($i%2) == 0){ $rowStyle = "oddrow"; $rowColor = "#e5e5e5"; } else { $rowStyle = "evenrow"; $rowColor = "#ffffff"; } print qq{ <tr class=$rowStyle style=Cursor:hand onclick= \"location.href=\'get +Collection.pl?id=$collectionData[$i]{'CollectionId'}\';\" onMouseOver +=\"style.backgroundColor='#c5c5c5'\" onMouseOut=\"style.backgroundCol +or='$rowColor'\"> <th class=graycenter>$sortedCollectionData[$i]{'CollectionId'}</th +> <th class=graycenter>$sortedCollectionData[$i]{'Framecount'}</th> <th class=graycenter>$longStatus</th> <th class=graycenter>$sortedCollectionData[$i]{'Missing'}</th> <th class=graycenter>$sortedCollectionData[$i]{'Modified'}</th> </tr> </div> }; } }

In reply to 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 browsing the Monastery: (3)
As of 2024-04-26 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found