Interesting TCL behavior: 007 == 7 is true; 008==8 is false.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Interesting TCL behavior: 007 == 7 is true; 008==8 is false.
# 1  
Old 11-04-2008
Interesting TCL behavior: 007 == 7 is true; 008==8 is false.

Hi all,
If anyone has the explanation for the following issue, please share it with me.

I am comparing two variable a and b with the values of 007 and 7, for these values it get evaluated as True. For a=008 and b=8, for these values it get evaluated as false.

#!/bin/tclsh
set a 007
set b 7
if { $a == $b } {
puts "$a equals to $b"
} else {
puts "$a not equals to $b"
}
set a 008
set b 8
if { $a == $b } {
puts "$a equals to $b"
} else {
puts "$a not equals to $b"
}

Output:
007 equals to 7
008 not equals to 8

In Fact a = (001,002,...007) and b= (1,2,...7) it get evaluated as True and becomes false for a > 007 and b > 7.

Thanks
Sarwan
# 2  
Old 11-04-2008
I'm not familiar with tclsh so I'm not sure about it, but it seems that it evalutes a value with a preceding 0 as an octet value.

Regards
# 3  
Old 11-04-2008
The observed behaviour is correct.

Quote:
A Tcl expression consists of a combination of operands, operators, and parentheses. White space may be used between operands, operators and parentheses; it is ignored by the expression processor. Where possible, operands are interpreted as integer values. Integer values may be specified in decimal (the normal case), in octal (if the first character of the operand is 0), or in hexadecimal (if the first two characters of the operand are 0x).
Changing the value of a to 010 (octal 10) in the second part of your example will cause the if statement to evaluate to true.
Code:
set a 010
set b 8

if { $a == $b } {
   puts "$a equals to $b"
} else {
   puts "$a not equals to $b"
}

# 4  
Old 11-05-2008
Hi fpmurphy and Franklin thanks for the answer. As per my program a and b are integers, "a" is not a octal, but I will get "a" as preceeded by 0, like 001,002,..008.. and "b" like 1,2...8...10. So now I had modified the program as follows.

set a 008
set b 8

if { [string length $b] == 1 } {
set b "00$b"
} elseif { [string length $b] == 2 } {
set b "0$b"
}

if { $a == $b } {
puts "$a equals to $b"
} else {
puts "$a not equals to $b"
}

Output:
008 equals to 008
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Output checker setting variable to TRUE or FALSE

Hi All, I'm trying to come up a way to check the output of some data i have. I need to be able to check for the order of the output and if its correct set a variable to false if it isnt. Currently the data is in the below format, this is the value which should cause the variable be set... (4 Replies)
Discussion started by: mutley2202
4 Replies

2. Shell Programming and Scripting

Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008)

Hi, I have the following requirement. There will be following text/line in a file (eg: search-build.txt) PRODUCT_VERSION="V:01.002.007.Build1234" I need to update the incremental build number (eg here 007) every time I give a build through script. I am able to search the string and get... (4 Replies)
Discussion started by: drwatson_droid
4 Replies

3. Solaris

True or false ? - Sun cluster 3.2 U3 questions...

I'm using clustered zones on my machine. i'm only at the test phase of my design and ultimately the oracle zones will be using VxVM. When the testing phase is complete, VxVM will be used in the containers. It is necessary for VxVM to run in the global zone for the containers to use it (is... (5 Replies)
Discussion started by: frustin
5 Replies

4. AIX

Interesting SMITTY behavior

I have a couple systems that are acting strangely. In 'smitty tcpip' everything is displayed twice. Even going into the submenus (like minimum configuration and startup) everything is displayed twice. Has anyone seen this? Know how to fix it? Thanks (3 Replies)
Discussion started by: pmmill2
3 Replies
Login or Register to Ask a Question