Title: RE: Sorting question
Erich Beyrent wrote, on Wed 12/1/2004 11:39
: > : I have the following data structure:
: > :
: > : $hash{$connid} = {"month"   => $month,
: > :                     "day"     => $day,
: > :                     "time"    => $time,
: > :                     "user"    => $user,
: > :                     "host"    => $host,
: > :                     "from_ip" => $from_ip,
: > :                    };
: > :
: > : The time (where time is in hh:mm:ss format) and there could be a few
entries
: > : with the same user and same timestamp - I only wish to display the first
: > : one.
: > :
: > : So this works just fine.  The problem is that I need to also sort the
: > : results by month, day, and time.  It looks as if the month and day fields
: > : are sorting, but not the time field, and I am unsure as to how to sort
: > this.
: >
: > What do you mean by "not the time field?" Perhaps you need to use "cmp"
: > instead of "<=>" (maybe you are; you :
: > didn't show any sorting code!) since there are embedded colons, or you
: > need to make sure your hours before 10
: > have leading zeros so they sort correctly ("hh:mm:ss" implies that they
: > already do, but maybe you didn't mean
: > that).
: >  
: > Joe
:
: Sorry for not being specific enough.  The data gets displayed with the times
: in no particular order.  I might have:
:
: 10:02:34
: 06:05:01
: 09:00:01
:
: Etc.  I agree that cmp would have to be used instead of <=>, but I am not
: sure as to how to implement this kind of sort on a single hash key when I am
: doing a foreach loop on all the keys.  Any assistance would be greatly
: appreciated!
So you want something like this?
 
foreach (sort { $hash{$a}{'month'} <=> $hash{$b}{'month'} ||
                $hash{$a}{'day'}   <=> $hash{$b}{'day'}   ||
                $hash{$a}{'time'}  cmp $hash{$b}{'time'} } keys %hash)
{
    # do stuff
}
 
Have I missed something in what you're doing? I'm afraid I no longer have your original message with your "do stuff" loop.
 
Joe
 

==============================================================
          Joseph P. Discenza, Sr. Programmer/Analyst
               mailto:[EMAIL PROTECTED]
 
          Carleton Inc.   http://www.carletoninc.com
          574.243.6040 ext. 300    fax: 574.243.6060
 
Providing Financial Solutions and Compliance for over 30 Years
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to