0

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, no braces around the $var3: if you add the space, the braces prevent variable substitution and you get the string "$var3" instead.
Thanks. removing brackets was the key as even after removing the space,I was getting an error
2

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.

1 Comment

such a useful tip...Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.