uberdruck
asked on
Perl Sort on associative arrays
Hello,
I want a sort on keys of a associative array. the keys are like 5, 10, 20, 30
I want to show them and the value associated to these keys, but in the ordinal order.
but my sort show: 10 20 30 5
because 1 2 3 5, it sorts this like string, but i want it sorted numericaly!
look at my code:
-------------------------- ---------- --------
my %liste_sequences = ();
while( $db->FetchRow() ) {
%hash = $db->DataHash;
$liste_sequences{$hash{ord re_afficha ge}} = $hash{sequence};
}
$compteur = 10;
foreach $ordre_aff (sort(keys(%liste_sequence s))) {
print "--> $ordre_aff <br>";
}
-------------------------- ---------- ---------
Thanx a lot
I want a sort on keys of a associative array. the keys are like 5, 10, 20, 30
I want to show them and the value associated to these keys, but in the ordinal order.
but my sort show: 10 20 30 5
because 1 2 3 5, it sorts this like string, but i want it sorted numericaly!
look at my code:
--------------------------
my %liste_sequences = ();
while( $db->FetchRow() ) {
%hash = $db->DataHash;
$liste_sequences{$hash{ord
}
$compteur = 10;
foreach $ordre_aff (sort(keys(%liste_sequence
print "--> $ordre_aff <br>";
}
--------------------------
Thanx a lot
You will need to create your own sort function or modify your keys. Personaly I would do the former. I would make the keys space filled numbers.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This is exactly the type of 'your own' sort function that I had in mind, except that you will need to ensure that the sort is a numeric sort and not a text/ascii character sort