Forum OpenACS Q&A: Problem wiht tcl`s control structure

Collapse
Posted by Irma Gamez on
Hello.

I´m writing a procedure with a for loop inside an if sentence. This is an example:

ad_proc -public sgra::ruta_opcion {
predefinida
sugeridas
} {
} {
set predef [llength $predefinida]
set sug [llength $sugeridas]
if {$predef < $sug} {
for {set i 0} {$i < $sug} {incr i}
{ puts "predef $predef" }
} else {
puts "sugerida $sug"
}
puts $sugeridas
}

I have the error:

wrong # args: should be "for start test next command"

while compiling
"for {set i 0} {$i < $sug} {incr i} "
("if" then script line 2)
while compiling
"if {$predef < $sug} {
for {set i 0} {$i < $sug} {incr i}

How can I do? Thank you very much.

Collapse
Posted by Emmanuelle Raffenne on
Hi Irma,

There are extra {} inside the for:

    { puts "predef $predef" }

It should be:

    puts "predef $predef"

Collapse
Posted by Emmanuelle Raffenne on
Irma,

Sorry, ignore my answer, I didn't read closely.

Collapse
Posted by Emmanuelle Raffenne on
Irma,

You need to put the opening brace of the command on the same line:

for {set i 0} {$i < $sug} {incr i} {
puts "predef $predef"
}

Collapse
Posted by Irma Gamez on
It works!
Really tricky. Thank you very much.
Collapse
Posted by Jim Lynch on
More generally...

TCL wants to see control structures all on one line, and there are weird exceptions to that... keep pluggin, you'll get used to it

-Jim

Collapse
Posted by Mark Aufflick on
The way I think of it is that a newline ends a tcl command, except inside a pair of {}. So unless you are inside a {} pair, a newline will cause tcl to attempt parse the statement, whether it is complete or not.

Also you can of course escape the newline with a backslash to continue a line.

Collapse
Posted by Irma Gamez on
Thank you very much.