Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2016-10-25 14:05 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (320 votes). Check out past polls.