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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with switch

Status
Not open for further replies.

sergelaurent

Technical User
May 30, 2005
52
0
0
FR
I have the following script!!! I expected that the getproc function returns "coucou" but it returns "et non".

/***************************************************/
set toto 156

proc getproc {chiffre} {
switch $chiffre {
$toto {set phrase "coucou"}
default {set phrase "et non"}
}
}

set tomi 156
set titi [getproc $tomi]
puts $titi
/***********************************************/
It seems to me that in the switch command, it does not evaluate the $toto!!!

Can someone tell me how to force switch to compare $chiffre with the content of the toto variable!!!
 
Either:
1 toto needs to be a global variable
Code:
proc getproc {chiffre} {
    [red]global toto[/red]
    switch $chiffre {
        $toto {set phrase "coucou"}
        default {set phrase "et non"}
    }
}

2 getproc needs to refer to toto in the calling scope
Code:
proc getproc {chiffre} {
    switch $chiffre {
        $[red]::[/red]toto {set phrase "coucou"}
        default {set phrase "et non"}
    }
}

3 getproc must receive toto as an argument
Code:
proc getproc {chiffre [red]toto[/red]} {
    switch $chiffre {
        $toto {set phrase "coucou"}
        default {set phrase "et non"}
    }
}

_________________
Bob Rashkin
 
Bong, this cannot work, since switch does not eval it's body.
The curly braces hinder the interpreter from substituting the value of $toto, whether locally or globally defined.

So the proc should look something like:
Code:
proc getproc {chiffre} {
    switch $chiffre [list \
        $::toto {set phrase "coucou"} \
        default {set phrase "et non"}\
    ]
}

Greetings,
Tobi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top