Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
switch and variables - no comparison/matching?

Posted by balk (balk), 20 July 2007
Love the site!  I do have a question that is troubling me.  

Is there a way to use a switch statement and have it look at the value of a variable for the comparison?  In the following switch statement, the switch doesn't recognize or match with $SegOne.  Even though I get SegmentType as MSH, it never hits the SegOne case.  I don't want to hardcode MSH in the switch as that would defeat the purpose of using a variable.

Any thoughts?  I'd appreciate it.

set SegOne MSH

set SegmentType [lindex $seg 0]

switch -exact -- $SegmentType {
 $SegOne {
   puts stdout "Inside with $SegOne"

 default {
   puts stdout "Outside with $SegmentType"

Posted by admin (Graham Ellis), 20 July 2007
Yes, Nate - you can.  The problem is the { on the end of the switch line which defines all the rest as a deferred block, so supresses the interpretation of the $SegOne.

You're probably going to ask me "so what should I do / how should I write it". Yikes - replace the outer { and } with " and " and add \ protection on every new line and " within ...   that's not trivial the first time!

Posted by balk (balk), 20 July 2007
Wow, that is crazy!  I tried it and it works.  I would've never got that one on my own.  It's nice that this Tcl forum actually gets some action and is being used.

Again, thank you for the response and your time.  It is appreciated.


This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

1 unpublished comment pending on this page

edit your own (not yet published) comments

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho