in reply to Re^2: ASCII Woe
in thread ASCII Woe
If you follow your "T=25,A=3,D=18" form, you can sort numerically easily enough. Let's say you wanted the data in order from lowest to highest temperature, and your data is in @data, with each element being a string of the above form:
my @sorted_by_temp_asc = sort { tricky_sort($a,$b,'T') } @data; sub tricky_sort { my ($A, $B, $field) = @_; my %a_dat = map { split('=', $_, 2) } split(',',$A); my %b_dat = map { split('=', $_, 2) } split(',',$B); return ( $a_dat{$field} <=> $b_dat{$field} ); }
The tricky_sort subroutine peforms a numerical comparison on the identified field, thus handling all the +/- sorting the way you want, and without too much twisted logic. Of course, you'll probably want to add more data validation and such before you put this into production -- after all <grail>it's only a model</grail> ;-)
And, it probably goes without saying, but to do a descending sort, just swap the order in which you pass $a and $b to tricky_sort
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: ASCII Woe
by QM (Parson) on Mar 05, 2006 at 22:50 UTC |