在 Tcl 中处理带前导零的数字

发布于 08-19 05:33 字数 307 浏览 11 评论 0 原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

溺孤伤于心 2024-08-26 05:33:20

您需要在 Tcl wiki 上阅读Tcl 和八进制数。规范的方法是将您的输入视为字符串并使用 scan 命令来提取数字。这导致了这个,是的,多行过程:

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
}

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:

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
}
紫竹語嫣☆ 2024-08-26 05:33:20

这是最干净的解决方案:

% expr [ scan "08" %d ] - 1
7

此外:

% expr [ scan "09" %d ]
9
% expr [ scan 09 %d ]
9
% set nine 09
09
% expr [ scan $nine %d ]
9
% set success [ scan $nine %d number ]
1
% puts $number
9

扫描这可能是您关心的。转换“09”(即使作为字符串)转换为十进制数 9

注意:
1. 如果该值无法转换为十进制值,则 scan 将返回空字符串 ({})。
2. 如果为 scan 命令提供变量名称(如上例中的 number 变量),则返回值指示状态0:解析十进制失败,1:成功。

expr :此处用于演示 expr 将值解释为数字,并解决原始问题。它计算一个表达式(例如 expr {2*3} 返回 6)。

This is the cleanest solution:

% expr [ scan "08" %d ] - 1
7

Furthermore:

% expr [ scan "09" %d ]
9
% expr [ scan 09 %d ]
9
% set nine 09
09
% expr [ scan $nine %d ]
9
% set success [ scan $nine %d number ]
1
% puts $number
9

scan : This is probably what you care about. Converts the "09" (even as a string) into a decimal number 9.

Note:
1. If this value cannot be converted into a decimal value, then scan will return an empty string ({}).
2. If a variable name is provided to the scan command (like the number variable in the example above), then the return value indicates status, 0: failed to parse decimal, and 1: success.

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} returns 6).

梦亿 2024-08-26 05:33:20

我的所有这些 Tcl/Tk 8.1 版程序都已损坏,因为 Tcl/Tk 8.5.10 无法正确处理字符串/数字转换。

在这里,shell 进入 Tcl,然后输入:(

 %  expr {01}
 1

等等..)

 %  expr {06}
 6

 %  expr {07}    
 7

,然后我们得到 8...

 %  expr {08}
 missing operator at "_@_"

看起来像无效的八进制数

,但情况会变得更糟。仍然在 Tcl shell 中,尝试以下操作:

在我的 Tcl8.1 shell 中:

 % format "%.0f" {08}
 8

但是在我新的和改进的 Tcl8.5 shell 中,我收到一个错误:

 % format "%.0f" {08}
 expected floating-point number but got "08" (looks like invalid octal number)

这太愚蠢了!
我的所有代码在 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:

 %  expr {01}
 1

(and so on..)

 %  expr {06}
 6

 %  expr {07}    
 7

and then we get to 8...

 %  expr {08}
 missing operator at "_@_"

looks like invalid octal number

But it gets worse. Still in Tcl shell, try this:

In my Tcl8.1 shell:

 % format "%.0f" {08}
 8

But in my new and improved Tcl8.5 shell, I get an error:

 % format "%.0f" {08}
 expected floating-point number but got "08" (looks like invalid octal number)

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.

雨后咖啡店 2024-08-26 05:33:20
set $clean_number [regsub {^0*(.+)} $troublesome_number {\1}] this doesnt work 
set clean_number "" 
set $troublesome_number 08
% set $clean_number [regsub {^0*(.+)} $troublesome_number {\1}]
wrong # args: should be "regsub ?switches? exp string subSpec varName"

一个更简单的解决方案是:

set x 08
regsub {^[0]} $x {\1} x
puts $x
=>8
set $clean_number [regsub {^0*(.+)} $troublesome_number {\1}] this doesnt work 
set clean_number "" 
set $troublesome_number 08
% set $clean_number [regsub {^0*(.+)} $troublesome_number {\1}]
wrong # args: should be "regsub ?switches? exp string subSpec varName"

an easier solution is:

set x 08
regsub {^[0]} $x {\1} x
puts $x
=>8
绅士风度i 2024-08-26 05:33:20

此链接应该可以帮助您

有效的八进制数只能包含数字0-7且必须以0开头

This link should help you

Valid octal numbers can only contain the digits 0-7 and must start with 0

匿名的好友 2024-08-26 05:33:20

就我个人而言,我一直使用:

set $clean_number [regsub {^0*(.+)} $troublesome_number {\1}]

来清理 $troublesome_number

Personally I've always used:

set $clean_number [regsub {^0*(.+)} $troublesome_number {\1}]

to sanitize $troublesome_numbers.

别把无礼当个性 2024-08-26 05:33:20
#!/bin/tclsh

#Regsub TCL script to remove the leading zeros from number. 
#Author : Shoeb Masood , Bagalore

puts "Enter the number"
set num [gets stdin]
regsub  {^0*} $num {\1} num
puts $num
#!/bin/tclsh

#Regsub TCL script to remove the leading zeros from number. 
#Author : Shoeb Masood , Bagalore

puts "Enter the number"
set num [gets stdin]
regsub  {^0*} $num {\1} num
puts $num
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文