Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

switch command

Status
Not open for further replies.

tomdagliusa

Programmer
May 31, 2000
34
US
I have some variables I have defined in a global values file. I want to use them as switch points in a switch command, but I can't get it to work.

The following type of code works:
switch -exact -- $port {
"1" {set if 400}
"2" {set if 500}
}

BUT, I don't want to hardwire the switch to values 1 and 2.

If I try to reference the variables, it doesn't work.
For instance:

switch -exact -- $port {
"$PORT_A" {set x 400}
"$PORT_B" {set x 500}
}

This method allows me to pre-set PORT_A and _B to whatever I need and not hardwire them to 1 or 2, BUT the switch falls thru and doesn't seem to recognize either. The variables port, PORT_A, and PORT_B are all declared so it's not that it can't "see" those vars.

Confused,
Tom

 
Remove the braces!
Code:
switch -exact -- $port "$PORT_A" {set x 400}  "$PORT_B" {set x 500}
is what you want.

From the Tcl Manual:

SYNOPSIS
switch ?options? string pattern body ?pattern body ...?
switch ?options? string {pattern body ?pattern body ...?}

HTH

ulis
 
I tried that, and I get a run time error wherein tcl can't
execute the command "6", which is what $PORT_A happens to be set to.

Tom
 
It sounds as though you either:[ol][li]forgot the backslashes at the end of the line to continue the command to the next line; or[/li][li]accidentally put a non-printing character, like a space or tab, after the backslash.[/li][/ol]When using the backslash at the end of a line to continue a command to the next line, it must be the very last character on the line. We are in essence "escaping" the newline. (Technically, when the Tcl interpreter parses the command, it replaces the backslash, newline, and all spaces and tabs immediately following the newline with a single space character.) - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
HI,

a question of scope perhaps.

Is the switch in a PROC, but the preset "constant" vars in the global scope of the program?

if so, the PROC must:

global PORT_A
etc.

let me know how it goes.

John Lopez
mailto:consulting4matrix@hotmail.com
John Lopez
Enterprise PDM & Integration Consulting
Development for MatrixOne PDM Systems
johnlopez2000@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top