in reply to Re: ASCII Woe
in thread ASCII Woe

Ah, someone took the bait :)

I'm not concerned for Perl, but for the next process in the pipeline. It's a data analysis/plotting program I need to feed data to. Some of the plots have a string of numbers in various places, like "25,3,18". This means something if you know that the first field is temperature (25C), the second field is attempt 3, and 18 is the day of the month.

I thought of trying to feed strings instead of numbers, which this system happily accepts. For instance, temperature could be "T=25", and the whole label is more informative. If the label looked like "T=25,A=3,D=18", those with mathaphobia didn't waste precious time working out which is which.

But some of the data have negative values. So "T=-25" sorts after "T=+25". Then some fields have one, two, and three digit values, so "T=10" sorts before "T=5". This can be solved with "T=05". But negative values don't work because of "+" vs. "-".

So I thought of using "N" for negative and "P" for positive. "T=N5" sorts before "T=P5". But "T=N05" sorts before "T=N10"!

My latest thought is to use a sequence number to order the values, because there aren't that many in each field. "0T=-10", "1T=-5", "2T=0", "3T=+5", etc. Since all values are known ahead of time, this encoding can be used to get proper ordering, and field name hints.

Maybe in version 72, this software package will allow a better mechanism for labelling data...

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

Replies are listed 'Best First'.
Re^3: ASCII Woe
by Aristotle (Chancellor) on Mar 03, 2006 at 17:42 UTC

    Prefix positive numbers with a “0” instead of a “+”.

    Won’t fix the sorting within negative numbers, of course.

    Makeshifts last the longest.

Re^3: ASCII Woe
by radiantmatrix (Parson) on Mar 03, 2006 at 19:01 UTC

    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
      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