Reputation: 699
I am learning TCL and found that I can initialize array as (k1,v1 is set of key values)
array set a2 {k1 v1 k2 v2}
however, when I try to use already existing list as
set var3 "k1 v1 k2 v2"
array set a2{$var3}
or
set var3 [listk1 v1 k2 v2]
array set a2{$var3}
I get an error:
wrong # args: should be 'array set arrayName list"
What am I doing wrong?
Upvotes: 0
Views: 1400
Reputation: 13252
Sometimes when a command invocation goes wrong it helps to look at the command as a list. In this way, one can see approximately how the command will look when it is ready to be executed.
list array set a2{$var3}
# -> array set {a2{k1 v1 k2 v2}}
Hmm, no, that doesn't look right. There's supposed to be four words, and that third word looks weird.
How about if I put a space before the left brace?
list array set a2 {$var3}
# -> array set a2 {$var3}
Close, but no cigar. It looks like the braces are interfering with the variable substitution. What if I remove them?
list array set a2 $var3
# -> array set a2 {k1 v1 k2 v2}
And there was much rejoicing.
In this manner, one can experiment with the command structure and the quoting until one gets it right. Just a tip.
Upvotes: 2
Reputation: 385830
You are missing a space between the array name and the value. The syntax is "array set name value" but you were calling it like "array set namevalue"
array set a2 $var3
In the other example you included brackets around the variable, which would prevent the variable from being expanded. ${var3}
will expand the same as $var3
, but {$var3}
will not.
Upvotes: 4