develooper Front page | perl.beginners | Postings from October 2009

Sorting mixed alphanumerics

Thread Next
Rick Triplett
October 14, 2009 06:53
Sorting mixed alphanumerics
Message ID:
I need to sort the keys in a hash. The keys are the question number  
and the values are the student's answer. A numeric sort with <=> won't  
work since retaking a missed question (say, 2) produces the new key,  
2h with its new answer. A representative hash might look like this

1 => b
2h => c
3 => a
2 => a

I chose the sort routine

{$a <=> $b || $a cmp $b}

which sorts correctly but gives me a warning about the occasional "h"  
not being numeric. I could suppress warnings for the sort routine or  
for the whole code, but I'd rather write better code, code that  
doesn't produce a warning. Unfortunately I haven't been able to figure  
out the correct code, despite looking through the sort sections of the  
standard books on Perl. Even though I'm a Perl novice, my intuition  
tells me that there is probably a simple way to do this. But several  
days of experimenting have been unsuccessful.

Advice appreciated!

Rick Triplett

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About