user1134991
user1134991

Reputation: 3103

How can I input variables into the Tcl switch code block?

I am trying to do something very simple, in Tcl 8.5. I have a variable, and another variable, and I am trying to equate them in a switch. What is very easily done in Python & Ruby, I could not get to work in Tcl. The manual page http://www.tcl.tk/man/tcl8.5/TclCmd/switch.htm does not help. See code & results:

set k 4
switch 4 {
    $k { puts "yey" }
    default {puts "no recon"  }
}
exit

It outputs:

no recon

Any ideas?

Upvotes: 0

Views: 665

Answers (1)

Dinesh
Dinesh

Reputation: 16428

Tcl won't do substitutions within braces. Change your code with double quotes as

set k 4
switch 4 "
    $k { puts yey }
    default {puts \"no recon\"  }
"

Or write it in a single line as

switch 4 $k { puts yey }  default {puts "no recon"  }

Upvotes: 1

Related Questions