On Tue, Oct 13, 2009 at 11:09:09AM -0500, Rick Triplett wrote:
> 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!

If the code is working then I think the simplest and best way is to
suppress that particular warning in that scope.  Remember that Perl is
supposed to be working for you and not the other way around.  A warning
is just that.  If you look at it and say thanks, but I know better in
this case, then just tell Perl to shut up.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to