in reply to Re: Inconsistent column Schwartzian Transform attempt
in thread Inconsistent column Schwartzian Transform attempt

...make sure you sprintf numbers with %0xxd, where the xx is enough space for the widest number in that column, and make sure you format strings with %-yys where yy is enough space for the widest string in that column.

That way, the fields will "cmp" correctly.

If you need keys sorted in a different order, just sprintf them differently. For descending numeric keys, sprintf 9999999-$fld. For descending string keys... Hmmm, don't have an easy answer to that one. Of course, as long as you don't need some string keys desc and others asc, you can just reverse the order of the cmp call in the sort block.

Hope this helps!
--
Mike

Replies are listed 'Best First'.
Re: And lexicographically means...
by Juerd (Abbot) on Mar 16, 2002 at 18:17 UTC

    If you need keys sorted in a different order, just sprintf them differently. For descending numeric keys, sprintf 9999999-$fld. For descending string keys... Hmmm, don't have an easy answer to that one.

    # Ascending sort { $a cmp $b } LIST sort { $a <=> $b } LIST # Descending sort { $b cmp $a } LIST sort { $b <=> $a } LIST
    I think using 9999999-$foo is an ugly way to do a descending sort, and I think sprintfing to sort numbers using cmp is evil, because there's already the <=> operator and you can use every routine you want for sorting - just reverse it if you want a descending sort.

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

      I'm building up the string key using sprintf just to avoid perl code in the comparison function.

      Evil yes. But also EFFICIENT.

      I'll bet for a long sort, it's probably faster to do a cmp than several cmps and <=>.

      But it's the weekend; I'm too lazy to fire up Benchmark. :)
      --
      Mike

Re: And lexicographically means...
by I0 (Priest) on Mar 16, 2002 at 18:46 UTC
    For descending string keys sprintf ~$fld