Chr*_*uns 6 parsing integer tcl octal
我在Tcl中使用带前导零的数字遇到麻烦.我正在解析一些可以有前导零的数字,例如"0012",它应该被解释为整数"十二".
$ tclsh
% set a 8
8
% set b 08
08
% expr $a - 1
7
% expr $b - 1
expected integer but got "08" (looks like invalid octal number)
Run Code Online (Sandbox Code Playgroud)
处理可能在Tcl中具有前导零的数字的最佳方法是什么?
在旁注中,如果"08"是无效的,那么在Tcl中构成有效八进制数的是什么?
你想在Tcl wiki 上阅读Tcl和Octal Numbers.规范的方法是将输入视为字符串,并使用scan命令提取数字.这导致了这个,是的多线,proc:
proc forceInteger { x } {
set count [scan $x %d%s n rest]
if { $count <= 0 || ( $count == 2 && ![string is space $rest] ) } {
return -code error "not an integer: \"$x\""
}
return $n
}
Run Code Online (Sandbox Code Playgroud)