在 Tcl 中处理带前导零的数字
我在 Tcl 中使用带前导零的数字时遇到问题。我正在解析一些可能有前导零的数字,例如“0012”,它应该被解释为整数“12”。
$ 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)
处理 Tcl 中可能有前导零的数字的最佳方法是什么?
顺便说一句,如果“08”是无效的,那么在 Tcl 中什么构成有效的八进制数?
I am having trouble in Tcl using numbers with leading zeros. I am parsing some numbers that can have leading zeros, such as "0012", which should be interpreted as the integer "twelve".
$ 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)
What is the best way to handle numbers that might have a leading zeros in Tcl?
On a side note, what would constitute a valid octal number in Tcl, if "08" is an invalid one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要在 Tcl wiki 上阅读Tcl 和八进制数。规范的方法是将您的输入视为字符串并使用
scan
命令来提取数字。这导致了这个,是的,多行过程:You'll want to read Tcl and Octal Numbers at the Tcl wiki. The canonical way is to treat your input as a string and use the
scan
command to extract the numbers. That leads to this, yes multiline, proc:这是最干净的解决方案:
此外:
扫描:这可能是您关心的。转换
“09”
(即使作为字符串)转换为十进制数9
。expr :此处用于演示
expr
将值解释为数字,并解决原始问题。它计算一个表达式(例如expr {2*3}
返回6
)。This is the cleanest solution:
Furthermore:
scan : This is probably what you care about. Converts the
"09"
(even as a string) into a decimal number9
.expr : Used here to demonstrate that
expr
interprets the value as a number, and addresses the original question. It evaluates an expression (e.g.expr {2*3}
returns6
).我的所有这些 Tcl/Tk 8.1 版程序都已损坏,因为 Tcl/Tk 8.5.10 无法正确处理字符串/数字转换。
在这里,shell 进入 Tcl,然后输入:(
等等..)
,然后我们得到 8...
看起来像无效的八进制数
,但情况会变得更糟。仍然在 Tcl shell 中,尝试以下操作:
在我的 Tcl8.1 shell 中:
但是在我新的和改进的 Tcl8.5 shell 中,我收到一个错误:
这太愚蠢了!
我的所有代码在 Tcl7.6 和 Tcl8.1 中都可以正常工作,但是开始给出
Tcl8.5 中奇怪的、随机的结果。只有当数字08恰好出现时
生成或使用!我花了几个小时试图找出问题所在。
但事实证明,这只是我正在使用的令人讨厌的代码!
因此,我发布此咆哮作为警告。
Tcl/Tk 版本 8.5.10 错误地处理数字 8。如果你是
期望您的格式语句有正常的行为,这种情况不会发生。
您的代码将继续前进,直到遇到值为 {08} 的字符串,并且
Tcl 8.5.10 解释器将生成错误,因为它会假设
{08} 是一个特殊情况的八进制数,无论所有
您使用过的其他小数字也可以很好地发挥作用!
上述问题的一种可能解决方案是降级回 Tcl 8.1
壳。我已经确认该版本至少可以处理格式
正确表述数字 08。 Tcl 8.5.10 shell 根本就没有。
I have all these Tcl/Tk version 8.1 programs that have become broken because Tcl/Tk 8.5.10 is not handling string/numeric conversions correctly.
Here, shell into Tcl, and type:
(and so on..)
and then we get to 8...
looks like invalid octal number
But it gets worse. Still in Tcl shell, try this:
In my Tcl8.1 shell:
But in my new and improved Tcl8.5 shell, I get an error:
This is just goofy!
I have all this code that works ok in Tcl7.6 and Tcl8.1, but which started giving
weird, random results in Tcl8.5. Only when the number 08 happened to
get generated or be used! I have spent hours trying to figure out the problem.
But it turns out it is just a nasty sack of code that I am using!
So, I am posting this rant as a warning.
Tcl/Tk Version 8.5.10 handles the number eight incorrectly. If you are
expecting sane behavior from your format statements, this will not happen.
Your code will fly along, until it encounters a string valued at {08}, and
the Tcl 8.5.10 interpreter will generate an error, because it will assume
that {08} is a special-case octal number, regardless of the fact that all the
other little numbers you have used will have worked just fine!
One possible solution to the problem mentioned above is to downgrade back to a Tcl 8.1
shell. I have confirmed that that version does at least handle format
statements for the number 08 correctly. Tcl 8.5.10 shell simply does not.
一个更简单的解决方案是:
an easier solution is:
此链接应该可以帮助您
有效的八进制数只能包含数字0-7且必须以0开头
This link should help you
Valid octal numbers can only contain the digits 0-7 and must start with 0
就我个人而言,我一直使用:
来清理
$troublesome_number
。Personally I've always used:
to sanitize
$troublesome_number
s.