Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
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...


Comment on Re: Dictionary-style sort a la Tcl?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-09-22 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (191 votes), past polls