string compare -dictionary?

  • Follow


The [lsort] command has a -dictionary option to sort list entries in
dictionary order, i.e.,
embedded numbers are sorted as numbers rather than as individual
characters.  Is the
underlying comparison available anywhere as a separate command?
There's no
[string compare -dictionary] command, although that's an obvious
enhancement
given [lsort -dictionary].

I mean, I could compare two strings by putting them in a list, sorting
the list
with -dictionary, and seeing which comes first...but that seems a
little inefficient
somehow.  Anybody got anything better?
0
Reply will181 (157) 11/17/2010 8:13:53 PM

On Nov 17, 9:13=A0pm, Will Duquette <w...@wjduquette.com> wrote:
> The [lsort] command has a -dictionary option to sort list entries in
> dictionary order, i.e.,
> embedded numbers are sorted as numbers rather than as individual
> characters. =A0Is the
> underlying comparison available anywhere as a separate command?
> There's no
> [string compare -dictionary] command, although that's an obvious
> enhancement
> given [lsort -dictionary].
>
> I mean, I could compare two strings by putting them in a list, sorting
> the list
> with -dictionary, and seeing which comes first...but that seems a
> little inefficient
> somehow. =A0Anybody got anything better?

Indeed, DictionaryCompare() is static in tclCmdIL.c, and is only used
by [lsort] and [lsearch].

TIPworthy :)

-Alex
0
Reply Alexandre 11/17/2010 9:40:32 PM


On 2010-11-17, Will Duquette wrote:
>
> The [lsort] command has a -dictionary option to sort list entries in
> dictionary order, i.e., embedded numbers are sorted as numbers rather
> than as individual characters.  Is the underlying comparison available
> anywhere as a separate command?


As far as I know, no.

It probably should be.


This is what I've been using:

| proc dictionary_compare {a b} {
|    lindex [lsort -dictionary -index 0 [list [list $a -1] [list $b 1]]] 0 1
| }


which is bloody horrible, but it's the shortest path between
"what you want" and "what you've got" that I've found so far.


--JE
0
Reply Joe 11/17/2010 9:59:41 PM

On Nov 17, 10:59=A0pm, Joe English <jengl...@flightlab.com> wrote:
> On 2010-11-17, Will Duquette wrote:
>
>
>
> > The [lsort] command has a -dictionary option to sort list entries in
> > dictionary order, i.e., embedded numbers are sorted as numbers rather
> > than as individual characters. =A0Is the underlying comparison availabl=
e
> > anywhere as a separate command?
>
> As far as I know, no.
>
> It probably should be.
>
> This is what I've been using:
>
> | proc dictionary_compare {a b} {
> | =A0 =A0lindex [lsort -dictionary -index 0 [list [list $a -1] [list $b 1=
]]] 0 1
> | }
>
> which is bloody horrible, but it's the shortest path between
> "what you want" and "what you've got" that I've found so far.

Funnily, in my tests the above is slightly slower than

   proc dictionary_compare {a b} {
    expr {[string equal $a [lindex [lsort -dictionary [list $a $b]]
0]]?-1:1}
   }

Possibly because, for the small strings I used, the extra [string
equal] is dominated by the list nesting overhead.

-Alex

0
Reply Alexandre 11/17/2010 10:10:05 PM

On Nov 17, 2:10=A0pm, Alexandre Ferrieux <alexandre.ferri...@gmail.com>
wrote:
> On Nov 17, 10:59=A0pm, Joe English <jengl...@flightlab.com> wrote:
>
>
>
>
>
> > On 2010-11-17, Will Duquette wrote:
>
> > > The [lsort] command has a -dictionary option to sort list entries in
> > > dictionary order, i.e., embedded numbers are sorted as numbers rather
> > > than as individual characters. =A0Is the underlying comparison availa=
ble
> > > anywhere as a separate command?
>
> > As far as I know, no.
>
> > It probably should be.
>
> > This is what I've been using:
>
> > | proc dictionary_compare {a b} {
> > | =A0 =A0lindex [lsort -dictionary -index 0 [list [list $a -1] [list $b=
 1]]] 0 1
> > | }
>
> > which is bloody horrible, but it's the shortest path between
> > "what you want" and "what you've got" that I've found so far.
>
> Funnily, in my tests the above is slightly slower than
>
> =A0 =A0proc dictionary_compare {a b} {
> =A0 =A0 expr {[string equal $a [lindex [lsort -dictionary [list $a $b]]
> 0]]?-1:1}
> =A0 =A0}
>
> Possibly because, for the small strings I used, the extra [string
> equal] is dominated by the list nesting overhead.
>
> -Alex

Thanks much, both.  I'll give 'em both a try.
0
Reply Will 11/17/2010 11:11:12 PM

Hi,

so there is already an isolated C function in tcl for comparing
strings in dictionary mode?

Wonderful!

So lets TIP!

Regards,

Martin

On 17 Nov., 22:40, Alexandre Ferrieux <alexandre.ferri...@gmail.com>
wrote:
> On Nov 17, 9:13=A0pm, Will Duquette <w...@wjduquette.com> wrote:
>
> > The [lsort] command has a -dictionary option to sort list entries in
> > dictionary order, i.e.,
> > embedded numbers are sorted as numbers rather than as individual
> > characters. =A0Is the
> > underlying comparison available anywhere as a separate command?
> > There's no
> > [string compare -dictionary] command, although that's an obvious
> > enhancement
> > given [lsort -dictionary].
>
> > I mean, I could compare two strings by putting them in a list, sorting
> > the list
> > with -dictionary, and seeing which comes first...but that seems a
> > little inefficient
> > somehow. =A0Anybody got anything better?
>
> Indeed, DictionaryCompare() is static in tclCmdIL.c, and is only used
> by [lsort] and [lsearch].
>
> TIPworthy :)
>
> -Alex

0
Reply MartinLemburg 11/18/2010 9:16:21 AM

5 Replies
412 Views

(page loaded in 0.796 seconds)

Similiar Articles:













7/23/2012 7:42:54 PM


Reply: