TCL 8.4没有lsort-nocase选项的解决方法



我正在使用-nocase标志执行一个简单的TCLlsort。然而,我运行该代码的一个系统仍然使用TCL 8.4,其中nocase不可用。有没有简单的解决方法,或者我必须手动处理?

TCL 8.4具有-dictionary标志,它提供不区分大小写的比较。如果你的字符串上没有数字,我认为行为等于-nocase标志。

来自文件:

-字典使用字典样式比较。这与-ascii相同,不同之处在于(a)除作为平局决胜符外,忽略大小写;(b)如果两个字符串包含嵌入的数字,则数字将作为整数而不是字符进行比较。例如,在-dictionary模式中,bigBoy在bigbang和bigBoy之间排序,x10y在x9y和x11y之间排序。

-nocase导致以不区分大小写的方式处理比较。如果与-dictionary、-integer或-real选项组合使用,则无效。

http://www.hume.com/html85/mann/lsort.html

这里有一个Schwartzian变换:

set lst {This is a Mixed Case sentence and this is the End}
set tmp [list]
foreach word $lst {lappend tmp [list $word [string tolower $word]]}
unset lst
foreach pair [lsort -index 1 $tmp] {lappend lst [lindex $pair 0]}
puts $lst

输出

a and Case End is is Mixed sentence the This this

编写自己的字符串比较过程:

proc nocaseCompare {a b} {
    set a [string tolower $a]
    set b [string tolower $b]
    if {$a < $b} {
        return -1
    } elseif {$a > $b} {
        return 1
    } else {
        return 0
    }
}

set lst {This is a Mixed Case sentence and this is the End}
puts [lsort -command nocaseCompare $lst]

输出:

a and Case End is is Mixed sentence the This this

最新更新