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

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://160164]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-06-23 05:06 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.