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 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.
Thanks,
Nate.



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.

Nate.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk 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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho