Mayank Goyal
Mayank Goyal

Reputation: 11

Variable access syntax issue

In the code below, I am getting an error while accessing ${data_${k}_store}. Can anyone help me with the proper syntax?

Error: can't read "data_${k": no such variable Use error_info for more info. (CMD-013)

enter image description here

Upvotes: 1

Views: 431

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137767

Usually, when you've got a complex name like that you're better off making an alias to it using upvar so that you can manipulate it with a simple name:

# The ‘0’ is for “current stack level”
upvar 0 data_${k}_store datastore
# Now any operation on ‘datastore’ is forwarded to data_${k}_store
puts $datastore

Upvotes: 2

TrojanName
TrojanName

Reputation: 5375

Yes, you're trying to reference a variable whose name itself is actually dynamic, and because you are using curly brackets the inner ${k} doesn't get evaluated. So instead of this

${data_${k}_store}

try this

[set data_${k}_store]

Upvotes: 1

Related Questions