Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bug in Tcl?

49 views
Skip to first unread message

walkabout

unread,
Apr 25, 2012, 8:45:59 PM4/25/12
to
I’m merely trying to strip off any leading “zeros” off of entered
numerical values (as Tcl may interpret as octal and expr & calc cmds
will both then throw an error).

For example… Why is 0012 and 0023 seen as > 1, but 0059 and 008 are
not?

The workaround was to use string trimleft $val 0, but the below
appears to illustrate unexpected behavior. I observed it happens under
regsub and scan commands, so if it is a bug, it likely resides deeper
in Tcl. Thanks...

set val 0012
# 0012
if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}
# 12
#
set val 0023
# 0023
if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}
# 23
#
set val 0059
# 0059
if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}
#
set val 0059.12
# 0059.12
if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}
# 59.12
#
set val 008
# 008
if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}
#
set val 008.1
# 008.1
if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}
# 8.1
#

blacksqr

unread,
Apr 26, 2012, 12:04:04 AM4/26/12
to
> set val 0012
> # 0012
> if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}
> # 12

Valid octal number. Greater than one.

> #
> set val 0023
> # 0023
> if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}
> # 23
> #

Ditto.

> set val 0059
> # 0059
> if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}
> #

Numeral 9 not allowed in octal number, evaluated as string. String
starting with 0 considered less than string starting with 1.

> set val 0059.12
> # 0059.12
> if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}
> # 59.12
> #

Decimal forces cast to floating-point number.

> set val 008
> # 008
> if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}
> #

Numeral 8 not allowed in octal number, etc.

> set val 008.1
> # 008.1
> if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}
> # 8.1
> #

Decimal forces cast, etc.

Harald Oehlmann

unread,
Apr 26, 2012, 1:38:12 AM4/26/12
to
On 26 Apr., 02:45, walkabout <telmat...@yahoo.com> wrote:
> I’m merely trying to strip off any leading “zeros” off of entered
> numerical values (as Tcl may interpret as octal and expr & calc cmds
> will both then throw an error).

I would use scanf for any number comming from "outside" (user input
etc):

Thus for integers:
if {1 == [scanf $val %d val]} {
# number is ok
}
and for floats:
if {1 == [scanf $val %f val]} {
# number is ok
}
Hope this helps,
Harald

M. Strobel

unread,
Apr 26, 2012, 4:40:29 AM4/26/12
to
replace scanf with scan.

Too much C can hurt...

/Str.

Erik Leunissen

unread,
Apr 26, 2012, 1:20:46 PM4/26/12
to
On 26/04/12 02:45, walkabout wrote:

>
> set val 0012
> # 0012
> if {$val>1} {regsub ^0+(.+) $val \\1 val;puts $val}

or:

if {$val>1} {set val [string trimleft $val 0];puts $val}

This might actually not be faster, since two commands are being
evaluated instead of one, but it reflects better the idea as it is
expressed in natural language.

Erik.
--
leunissen@ nl | Merge the left part of these two lines into one,
e. hccnet. | respecting a character's position in a line.

Erik Leunissen

unread,
Apr 26, 2012, 1:26:15 PM4/26/12
to
On 26/04/12 19:20, Erik Leunissen wrote:
>
> if {$val>1} {set val [string trimleft $val 0];puts $val}
>

Which the OP already mentioned. Sorry for being unattentive.

Erik.
0 new messages