why doesn't perl sort the hash key in numeric order ?

Posted by Haiyuan Zhang on Stack Overflow See other posts from Stack Overflow or by Haiyuan Zhang
Published on 2010-03-09T09:59:02Z Indexed on 2010/03/09 10:06 UTC
Read the original article Hit count: 199

Filed under:
#!/usr/bin/perl
use strict;
use warnings;

my %hash;
foreach ( 1 .. 10 ) {
    $hash{$_} = $_;
}
foreach ( sort(keys %hash) ) {
    print $_ . ":  " . "$hash{$_}" . "\n" ;
}

execute the above code, the result is as below :

 1:  1
 10:  10
 2:  2
 3:  3
 4:  4
 5:  5
 6:  6
 7:  7
 8:  8
 9:  9

Yes, I expect "10: 10" to be the last one taht is printed . So I just need someone to explain why perl give me surprise in this case.

© Stack Overflow or respective owner

Related posts about perl