in reply to sorting a hash by key

You're sorting alphatically. Use
foreach $number (sort { $a <=> $b } keys(%name)) {
to sort numerically.