From: Uwe Klein on
Rohit M wrote:
> On Apr 20, 9:07 pm, Donald G Porter <d...(a)nist.gov> wrote:
>
>>Rohit M wrote:
>>
>>> if { [expr {$numeralVal1} ] > [expr {$numeralVal2}] } {
>>
>>Please find whoever taught you that and tell them never to teach
>>Tcl again.
>>
>>DGP
>
>
> I have not one to blame DGP, self taught. But I know the code is
> wrong,
> written hurriedly, dont think there is need to do expr as we already
> have pure numbers.

[if] and [while] do the "expression test" via [expr].
any additional embedded use of [expr] is unnecessary.

uwe

From: Glenn Jackman on
At 2010-04-21 02:22AM, "Donald Arseneau" wrote:
> 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}]
> }
> }

Nice. Thanks.


--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
From: Rohit M on
On Apr 21, 5:38 pm, Glenn Jackman <gle...(a)ncf.ca> wrote:
> At 2010-04-21 02:22AM, "Donald Arseneau" wrote:
>
>
>
> >  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}]
> >           }
> >       }
>
> Nice.  Thanks.
>
> --
> Glenn Jackman
>     Write a wise saying and your name will live forever. -- Anonymous

Thanks to all the wise tcl masters!
Donald A, I think "indices" option is available only in 8.5 right?
From: Larry W. Virden on
Here's a tweaked version of your original code. I tried to reduce the
code to achieve the philosophy of the original. However, the retVal is
< 0, 0, or > 0 rather than specifically -1, 0, 1 .

Most sorting routines only expect < 0 though.

set loop 100000
set timeVal [time {
set value1 "ABC (52)"
set value2 "ABC (452)"

set splitList1 [ split $value1 " ()"]
set splitList2 [ split $value2 " ()"]

set alphaVal1 [lindex $splitList1 0]
set alphaVal2 [lindex $splitList2 0]
set retVal [string compare $alphaVal1 $alphaVal2]
set numeralVal1 {}
set numeralVal2 {}

if { $retVal == 0 } {

set numeralVal1 [lindex $splitList1 2]
set numeralVal2 [lindex $splitList2 2]

# If numeralVals are different we need to compare them also
set retVal [ expr {$numeralVal1 - $numeralVal2}]
}
# puts "return is: $alphaVal1, $alphaVal2, $numeralVal1,
$numeralVal2, $retVal"
} $loop ]
puts "time: $timeVal"
From: Rohit M on
On Apr 21, 9:39 pm, "Larry W. Virden" <lvir...(a)gmail.com> wrote:
> Here's a tweaked version of your original code. I tried to reduce the
> code to achieve the philosophy of the original. However, the retVal is
> < 0, 0, or > 0 rather than specifically -1, 0, 1 .
>
> Most sorting routines only expect < 0 though.
>
>     set loop 100000
>     set timeVal [time {
>     set value1 "ABC (52)"
>     set value2 "ABC (452)"
>
>     set splitList1 [ split $value1 " ()"]
>     set splitList2 [ split $value2 " ()"]
>
>     set alphaVal1 [lindex $splitList1 0]
>     set alphaVal2 [lindex $splitList2 0]
>     set retVal [string compare $alphaVal1 $alphaVal2]
>     set numeralVal1 {}
>     set numeralVal2 {}
>
>     if { $retVal == 0 } {
>
>      set numeralVal1 [lindex $splitList1 2]
>      set numeralVal2 [lindex $splitList2 2]
>
>     # If numeralVals are different we need to compare them also
>      set retVal [ expr {$numeralVal1 - $numeralVal2}]
>     }
>      # puts "return is: $alphaVal1, $alphaVal2, $numeralVal1,
> $numeralVal2, $retVal"
>     } $loop ]
>     puts "time: $timeVal"

Thanks Larry, yes it is better but I dont think it as fast as lsort
simply because I guess "split" takes too much time.