Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

string compare -dictionary ?

4 views
Skip to first unread message

Jan Kandziora

unread,
Dec 18, 2009, 11:30:23 AM12/18/09
to
Hi,

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

Jan Kandziora

unread,
Dec 18, 2009, 11:54:29 AM12/18/09
to
Jan Kandziora schrieb:

>
> 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.
>
Ok, I found a solution myself but I'm not happy with it. It's ugly...

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

bs

unread,
Dec 18, 2009, 1:32:54 PM12/18/09
to

[string compare $a $b] doesn't work for you?

Tcl Bliss

unread,
Dec 18, 2009, 1:37:59 PM12/18/09
to
On Dec 18, 8:54 am, Jan Kandziora <j...@gmx.de> wrote:

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}
}

Jan Kandziora

unread,
Dec 18, 2009, 1:41:55 PM12/18/09
to
bs schrieb:

>
> [string compare $a $b] doesn't work for you?
>
[string compare] does ascii comparision, not dictionary. In a dictionary,
numbers are sorted as numbers.

% 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

Tcl Bliss

unread,
Dec 18, 2009, 1:57:29 PM12/18/09
to
Sqlite does English collation by default. Why are you creating your
own?

Bruce Hartweg

unread,
Dec 18, 2009, 3:13:57 PM12/18/09
to
Tcl Bliss wrote:
> On Dec 18, 8:54 am, Jan Kandziora <j...@gmx.de> wrote:
>> Jan Kandziora schrieb:
>>
>>> 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.
>> Ok, I found a solution myself but I'm not happy with it. It's ugly...
>>
>> 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
>
> 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:
>
less verbose by about 6 chars total, and also completely wrong ;)

> 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

Tcl Bliss

unread,
Dec 18, 2009, 4:01:13 PM12/18/09
to

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. :)

Jan Kandziora

unread,
Dec 20, 2009, 7:34:08 AM12/20/09
to
Tcl Bliss schrieb:

>
> Sqlite does English collation by default. Why are you creating your
> own?
>
It doesn't do english collation but binary collation. It's nearly the same
with ascii chars but anyway: I don't need ascii collation, I need
dictionary collation so numbers within strings are treated as numbers. See

% 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:

Tcl Bliss

unread,
Dec 21, 2009, 5:36:20 PM12/21/09
to

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.

0 new messages