Shikha Rustagi
Shikha Rustagi

Reputation: 15

How to use an array in switch statements?

Is it allowed to use an array element using tcl switch statement? I have an array named

set name "pad"
set p_list("pad") "IO2"

and now trying to do some conditional based stuff on that array like

 % switch $p_list($net_name) {
      IO1 {
           puts "io1"
      }
      IO2 {
         puts "io2"
      }
      default {
         puts "default"
      }
    }

but it seems array can't be passed to switch and therefore, erroring out with following error can't read "pads_list(pad)": no such element in array

Could someone please suggest if it's allowed or not. If, allowed then how can I make it work?

Upvotes: 0

Views: 164

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137637

Double quotes are not special in Tcl when not at the start or end of a word. Tcl is not Python or C. This means that you've made an array element with the name "pad", including the double quotes. That's legal, but quite unusual in the sort of application you're writing. (The switch you've written looks absolutely fine.) Either you should write:

set p_list(pad) "IO2"

or:

set net_name "\"pad\""

I'm guessing that the former is the one you want.

Possibly also:

set p_list($net_name) "IO2"

And you can change around variable names too.


There are some other options too:

set "p_list(pad)" "IO2"
set net_name {"pad"}

I don't think these are likely to make you happy, but they'll do the right thing.

Upvotes: 1

Related Questions