Solved

Perl Sort on associative arrays

Posted on 2003-11-16
3
1,112 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 125
perl split by | 2 90
stftime format 4 55
Get a Perl script to return content from a module 7 78
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

929 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

13 Experts available now in Live!

Get 1:1 Help Now