From: Donald Arseneau on
On Apr 21, 7:34 am, Rohit M <rohit.marka...(a)gmail.com> wrote:
> Donald A, I think "indices" option is available only in 8.5 right?

Ah yes, I have forgotten when it came in, but it isn't
in 8.4.13.

From: Schelte Bron on
Glenn Jackman wrote:
> Is that the best way to examine list equality? I also considered
> examining them as strings:
>
> {"$s1 $s2" eq [join [lsort -dictionary [list $s1 $s2]]]}
>
Fortunately you don't need to examine list equality at all. You are
working with a two-element list where you have already established
that the two elements are different. So you can just check the first
element against the original first element to know for sure whether
the elements have swapped places or not:

if {[lindex [lsort -dictionary [list $s1 $s2]] 0] eq $s1} {
return -1
} else {
return 1
}


Schelte

From: Donald Arseneau on
On Apr 20, 12:17 pm, Glenn Jackman <gle...(a)ncf.ca> wrote:

>     proc string_comp_dictionary {s1 s2} {
>         if {$s1 eq $s2} {
>             return 0
>         } else {
>             set l [list $s1 $s2]
>             if {$l == [lsort -dictionary $l]} {
>                 return -1
>             } else {
>                 return 1
>             }
>         }
>     }

You don't need to test list equality, fortunately, because
there is the -indices option:

proc string_comp_dictionary {s1 s2} {
if {$s1 eq $s2} {
return 0
} else {
set il [lsort -dictionary -indices [list $s1 $s2]]
return [expr {[lindex $il 0] ? -1 : 1}]
}
}

From: Rohit M on
On Apr 21, 9:20 am, Rohit M <rohit.marka...(a)gmail.com> wrote:
> On Apr 21, 12:17 am, Glenn Jackman <gle...(a)ncf.ca> wrote:
>
>
>
> > At 2010-04-20 12:20PM, "Donald G Porter" wrote:
>
> > >  Uwe Klein wrote:
> > > > I am too dumb to compare strings.
> > > > But I can sort them:
>
> > >  Why not use [lsort -dictionary] if the aim is sorting?
>
> > >  The [lsort -dictionary] command could also be the core of
> > >  a pairwise comparison function, but I'll leave it to other
> > >  to test whether the performance of such a thing is acceptable
> > >  and/or better than other alternatives.
>
> > Here's an implementation
>
> >     proc string_comp_dictionary {s1 s2} {
> >         if {$s1 eq $s2} {
> >             return 0
> >         } else {
> >             set l [list $s1 $s2]
> >             if {$l == [lsort -dictionary $l]} {
> >                 return -1
> >             } else {
> >                 return 1
> >             }
> >         }
> >     }
>
> > Is that the best way to examine list equality?  I also considered
> > examining them as strings:
>
> >     {"$s1 $s2" eq [join [lsort -dictionary [list $s1 $s2]]]}
>
> > --
> > Glenn Jackman
> >     Write a wise saying and your name will live forever. -- Anonymous
>
> Thanks Glenn, I think this would be faster that what I have.


From: Rohit M on
On Apr 21, 9:20 am, Rohit M <rohit.marka...(a)gmail.com> wrote:
> On Apr 21, 12:17 am, Glenn Jackman <gle...(a)ncf.ca> wrote:
>
>
>
> > At 2010-04-20 12:20PM, "Donald G Porter" wrote:
>
> > >  Uwe Klein wrote:
> > > > I am too dumb to compare strings.
> > > > But I can sort them:
>
> > >  Why not use [lsort -dictionary] if the aim is sorting?
>
> > >  The [lsort -dictionary] command could also be the core of
> > >  a pairwise comparison function, but I'll leave it to other
> > >  to test whether the performance of such a thing is acceptable
> > >  and/or better than other alternatives.
>
> > Here's an implementation
>
> >     proc string_comp_dictionary {s1 s2} {
> >         if {$s1 eq $s2} {
> >             return 0
> >         } else {
> >             set l [list $s1 $s2]
> >             if {$l == [lsort -dictionary $l]} {
> >                 return -1
> >             } else {
> >                 return 1
> >             }
> >         }
> >     }
>
> > Is that the best way to examine list equality?  I also considered
> > examining them as strings:
>
> >     {"$s1 $s2" eq [join [lsort -dictionary [list $s1 $s2]]]}
>
> > --
> > Glenn Jackman
> >     Write a wise saying and your name will live forever. -- Anonymous
>
> Thanks Glenn, I think this would be faster that what I have.

Indeed improvements are there, here are wost case timings over 100
thousand interations:
My code old code: 27 microseconds per iteration
Glenn's code: 6 microseconds per iteration