to implement a new collation scheme for sqlite, I need to compare two string
values the way [lsort -dictionary] does. I have looked for it but it
doesn't seem to be exposed anywhere.
Kind regards
Jan
proc string_compare_dictionary {a b} \
{
if {$a eq $b} {return 0}
if {[ lindex [lsort -dictionary -indices [list $a $b]] 0]} \
{return 1} {return -1}
}
Kind regards
Jan
[string compare $a $b] doesn't work for you?
I find it elegant enough, it is not ugly. It is simple, short and it
works. You can make it slightly less verbose like this:
proc string_compare_dictionary {args} \
{
if {$a eq $b} {return 0}
if {[ lindex [lsort -dictionary -indices $args] 0]} \
{return 1} {return -1}
}
% string compare a1 a2
-1
% string compare a11 a2
-1
So a11 is "smaller" than a2. Fine, but not what I want. See
% lsort -ascii [ list a11 a2 ]
a11 a2
% lsort -dictionary [ list a11 a2 ]
a2 a11
Kind regards
Jan
> proc string_compare_dictionary {args} \
> {
> if {$a eq $b} {return 0}
a and b are undefined now
> if {[ lindex [lsort -dictionary -indices $args] 0]} \
> {return 1} {return -1}
> }
so if you change to args you would need to change the initial
check to
if {[lindex $args 0] eq [lindex $args 1]} {return 0}
which is an increase of 28 chars!, and you also let
someone call it with more than 2 args which is meaningless
and could hide an error in other code.
Bruce
My fault. Didn't catch that one. :)
How about this:
if {[string equal -nocase $a $b]} {return 0};
or
if {[string equal -nocase {*}$args]} {return 0};
# -nocase makes it a better choice to {$a eq $b}, if case
insensitivity is needed.
I don't have a problem with "args" since in this case I am the one in
control of the caller and the callee procs.
I should have said "more readable", not "less verbose". I really
didn't count the characters. But that's just my opinion. :)
% string compare a1 a2
-1
% string compare a11 a2
-1
So a11 is "smaller" than a2. Fine, but not what I want. Here is what I want:
Yes, you are right. Well, it looks like you are pretty much at the
same or similar code you started with. I don't see any problems using
any code suggested. If someone can come up with something shorter or
more creative, it would be nice to see. But it isn't something
critical or important. If it works, it's fine.