Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Dictionary-style sort a la Tcl?

by ariels (Curate)
on Apr 18, 2002 at 10:33 UTC ( #160164=note: print w/replies, xml ) Need Help??


in reply to Dictionary-style sort a la Tcl?

You need to write a sub (dictionary?) to pass to sort. Here's an attempt, based on your short explanation...
#!/usr/local/bin/perl -w use strict; sub dictionary { dictionary_case(lc $a,lc $b) || dictionary_case($a,$b) } sub dictionary_case { my @a = split /(\d+)/, $_[0]; my @b = split /(\d+)/, $_[1]; my $lex = 1; for(;;) { my $x = shift @a; my $y = shift @b; if (! defined $x) { return -(defined $y); } elsif (! defined $y) { return +1; } my $c = $lex ? $x cmp $y : $x <=> $y; $lex = !$lex; return $c if $c; } return 0; } print join "\n", sort dictionary (qw(X10y x9y x bigboy y x11y bigbang bigBoy)), "\n";

It probably doesn't do what you'd expect for comparing "bigboy" and "big9boys", but your specification seems quiet on the subject...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://160164]
help
Chatterbox?
[marinersk]: Ah, you beat me to it.
[LanX]: as long as he has votes left, the nodelet remains
[LanX]: There is a very simple solution ...
[marinersk]: Correct, so one workaround is to leave one vote.
[marinersk]: But I was looking for a more elegant solution. It appears noone online at this time is aware of one.
[LanX]: go to Nodelet Settings and click "All nodelets off"
[LanX]: SUPER STABLE!!!
[marinersk]: LanX++ LOL Yes, that is another workaround. :-)

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2017-05-29 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?