Prince99 has asked for the wisdom of the Perl Monks concerning the following question:
foreach $record ( sort { $a->{$sort_by} cmp $b->{$sort_by} || $a- +>{$sort_by} <=> $b->{$sort_by} } @claim_records ) { print STDOUT $record->{'claim_number'}, "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting a hash
by jeroenes (Priest) on Apr 11, 2001 at 17:54 UTC | |
| [reply] | |
|
Re: Sorting a hash
by davorg (Chancellor) on Apr 11, 2001 at 17:59 UTC | |
Maybe you could try putting the numeric comparison first, as that's most likely to return 0. --<http://www.dave.org.uk>
"Perl makes the fun jobs fun | [reply] |
|
(tye)Re: Sorting a hash
by tye (Sage) on Apr 11, 2001 at 21:01 UTC | |
For some fairly simple answers to this problem, see my responses to How do I do a natural sort on an array?. I plan to update these and add some more answers but I haven't gotten back to that yet, sorry. - tye (but my friends call me "Tye") | [reply] |
|
Re: Sorting a hash
by stephen (Priest) on Apr 11, 2001 at 20:36 UTC | |
A few things going on here. First, I added 'use strict', which will throw a bunch of errors around if anyone tries to use variables improperly. Next, I turned CLAIM_KEYS into a list of lists called @Claim_Fields. (I could have used a constant here, but one strange thing at a time.) Later on, I'm going to need to look up what type of sortings is needed on a given field, so I call get_table() to get a hash table. We'll define get_table() further down. Made some changes here. First, you had $data_file (formerly $DAT) and $sort_by hard-coded in your script. If you use @ARGV, you can specify them on the command line when you run the script. I added a '$!' to your die message on opening the data file, so that if it fails, it will print a more useful error message. Also, I moved your open and close out of subroutines, since it's good practice to close your handles in the same routine in which you opened them.
One of the things that was failing in your original script was that @claim_records wasn't getting passed to @sort_file. I turned $sort_by and @claim_records into arguments. Look at perlman:perlsub. The first line reads $sort_by and @claim_records in as arguments. Then, we look at the %Claim_Table which we initialized at the top of the program and get the sort type. Next, we define an anonymous subroutine (see perlman:perlsub again) based on the 'L' or 'N' value in the table up top. If it's 'L', our subroutine will sort lexically, 'N' numerically. Well, it'll actually sort lexically if the value is anything other than 'N', but close enough. Finally, we close up with the original read_record() method, modified slightly to call the new get_fields() and get_template() subroutines. See perlman:perlref for more about handling lists of lists. Anyway, that should get you out of the mess I put you into with my last post. :) Best of luck. stephen | [reply] [d/l] [select] |
|
Re: Sorting a hash
by suaveant (Parson) on Apr 11, 2001 at 18:25 UTC | |
Give us some examples of your data and how it would sort appropriately... - Ant | [reply] |
by Prince99 (Scribe) on Apr 11, 2001 at 18:56 UTC | |
Update: The claim_number example data includes: 003549 000081 004951 001249 000059 Hope this help. Prince99 Too Much is never enough... | [reply] [d/l] |
by suaveant (Parson) on Apr 11, 2001 at 20:40 UTC | |
you may want to move your sort code into a subroutine and just call sort with the sub name instead of direct code, make your code easier to read since the code in the sort is getting long - Ant | [reply] [d/l] |