Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
XP is just a number
 
PerlMonks  

Re^2: Dictionary-style sort a la Tcl?

by Rohaq (Initiate)
on Feb 02, 2012 at 15:09 UTC ( #951460=note: print w/ replies, xml ) Need Help??


in reply to Re: Dictionary-style sort a la Tcl?
in thread Dictionary-style sort a la Tcl?

Wow, I feel like the ultimate necroposter here.

I stumbled across this solution, and found it fell over when it met the likes of the following:

qw{ 1.01 1.3 1.02 1.2 }
$A <=> $B does a numerical comparison, but strips any leading zeroes from the number, so how 1.02 and 1.2 are sorted will depend on their original order in the list - Tcl's dictionary sort doesn't seem to do this, and will place 1.2 above 1.02 in the sort.

So I extended your example to check for the string length of the compared numbers if they match numerically - If they match numerically, but have differing string lengths, then one must have leading zeroes. I also implemented the code into a subroutine:

sub dict_sort { my @unsorted = @_; my @sorted = map $_->[0], sort { my $i = 0; { my $A = $a->[1][$i]; my $B = $b->[1][$i]; defined($A) || defined($B) # Stop if both undef and ( defined($A) <=> defined($B) # Defined wins over undef or ( $A !~ /\d/ || $B !~ /\d/ # $A or $B is non-integer ? (lc $A cmp lc $B) # ?? Stringy lowercase || ( $A cmp $B) # -> Tie breaker : $A <=> $B # :: $A and $B are integers or ( length($A) <=> length($B) # If numeric comparison ret +urns the same, check length to sort by leading zeroes ) ) or ++$i && redo # tie => next part ); } } map [ $_, [ split /(\d+)/ ] ], @unsorted; return @sorted; }

I'm not sure that this thread will ever get any more posts, but hopefully this helps somebody out!


Comment on Re^2: Dictionary-style sort a la Tcl?
Select or Download Code
Re^3: Dictionary-style sort a la Tcl?
by Anonymous Monk on Feb 04, 2012 at 09:39 UTC

    I stumbled across this solution, and found it fell over when it met the likes of the following: qw{ 1.01 1.3 1.02 1.2 };

    $A <=> $B does a numerical comparison, but strips any leading zeroes from the number, so how 1.02 and 1.2 are sorted will depend on their original order in the list

    No, the spaceship operator  <=> does no stripping of any kind, it does a numerical comparison, and numbers don't have leading zeros , 0002 is 02 is 2

    The problem is with the code/regex, which turns decimals into an array of integers , and then does a numerical comparison on each portion -- not gonna work

    I'm not too familiar with Tcl's sort order, but what works for me (esp for mp3s) is to lowercase the string, and pad all digits with zeroes (sometimes 6, sometimes 20)

    #!/usr/bin/perl -- use strict; use warnings; my @list = ( qw{ bigBoy bigbang bigboy x10y x9y x11y 1.01 1.3 1.02 1.2 x1.1y x1.01y x2.3y x2.1y } ); print join "\n", map { $$_[0] } sort { $$a[1] cmp $$b[1] } map { my $f = $_; $f =~ s/(\d+)/sprintf '%06d',$1/ge; [ $_, lc $f ] } @list; __END__ 1.01 1.02 1.2 1.3 bigbang bigBoy bigboy x1.1y x1.01y x2.1y x2.3y x9y x10y x11y
      Sorry, I guess I could have worded that better - it doesn't 'strip' leading zeroes, it just ignores them as it's doing a numerical comparison. In any case, the change I added seems to make the sort identical to the Tcl dictionary sort. I'm not sure about yours.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://951460]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-04-20 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls