user3016320
user3016320

Reputation: 59

how to fix error:missing close brace error in tcl?

for { set i 0.05 } { $i < 0.5 } { set i [expr { $i+0.05 } ] } {
    puts "I inside first loop: $i"
} 

I will get below error when I run above lines of code Error: missing close-brace

what is the issue here?

Upvotes: 0

Views: 9400

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137557

That code “works” for me.

% for { set i 0.05 } { $i < 0.5 } { set i [expr { $i+0.05 } ] } {
    puts "I inside first loop: $i"
} 
I inside first loop: 0.05
I inside first loop: 0.1
I inside first loop: 0.15000000000000002
I inside first loop: 0.2
I inside first loop: 0.25
I inside first loop: 0.3
I inside first loop: 0.35
I inside first loop: 0.39999999999999997
I inside first loop: 0.44999999999999996
I inside first loop: 0.49999999999999994

I'm guessing that the real code that you're having a problem with isn't exactly identical to it, and that it's missing a } somewhere. The best way to hunt such things down is to use a programmer's editor that does bracket matching (they virtually all do; I know for sure that vim, emacs and Eclipse do this) and to see where your code has a bracket (or parenthesis or brace) that doesn't match up with what you expect it to. You can then dive inwards to find the innermost bracket that doesn't match up with what you want; it will probably be it's pair that is missing.

FYI, Tcl is strict about {} matching, fairly strict about [] matching, and usually pretty easy about () matching (though expression and array variable handling might disagree).


Your loop would be better written as:

for {set ii 1} {[set i [expr {$ii * 0.05}]] < 0.5} {incr ii} {
    puts "I inside first loop: $i"
}

or even:

for {set ii 1} {$ii < 10} {incr ii} {
    set i [expr {$ii / 20.0}]
    puts "I inside first loop: $i"
}

The reasons have to do with the way computers perform floating point arithmetic, and are not special to Tcl at all; you have the identical problem in C and C++ and Java and C# and …

Upvotes: 1

Related Questions