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

<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

Replies are listed 'Best First'.
Re^4: ASCII Woe
by QM (Parson) on Mar 05, 2006 at 22:50 UTC
    I appreciate what you did. However, what I didn't fully explain is that the 3 fields are joined in the downstream application. These values are used to label plots, and the plots are sorted either as strings or numbers (the application works like Perl in this sense). Some plots are displayed side by side, so a progression of temperature or voltage from left to right or top to bottom is very helpful.

    If I had the choice of sorting it myself, it would be easy. Instead, I have to make the strings sort as numbers, by fudging the strings.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of