Solved

Perl Sort on associative arrays

Posted on 2003-11-16
3
1,110 Views
Last Modified: 2008-01-16
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{ordre_affichage}} = $hash{sequence};
      }

      $compteur = 10;
      foreach $ordre_aff (sort(keys(%liste_sequences))) {
                  print "--> $ordre_aff <br>";
        }
---------------------------------------------

Thanx a lot
0
Comment
Question by:uberdruck
  • 2
3 Comments
 
LVL 8

Expert Comment

by:jhurst
ID: 9760844
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.
0
 
LVL 11

Accepted Solution

by:
bcladd earned 250 total points
ID: 9760895
Try the following in your sort loop:

 foreach $ordre_aff (sort {$a <=> $b} (keys(%liste_sequences))) {
               print "--> $ordre_aff <br>";
        }

(from the examples on the perlfunc page for the sort function).

-bcl
0
 
LVL 8

Expert Comment

by:jhurst
ID: 9761262
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
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now