0

In TCL (running v8.6.6) I want to create an array with a substitution of one or more of the value of the array with the value of another variable.

If we were in C I will write

float a = 10;
float b[4] = {1.0, 2.0, 3.0, 4.0};
b[2]=a;

and if I print on stdout I got 1.0 10.0 3.3 4.0.

In TCL instead I started by a simple exampe. I wrote

set a 10.0
set b $a

puts $b

I got 10.0 as output, but if I want to do the same in a array so I wrote

set a 10.0
set b {1.0 $a 3.0 4.0}

puts $b

and I'm expecting

1.0 10.0 3.0 4.0

but I got

-0.5 $a 0.5 0.79

Any idea?

Thanks a lot

5
  • 1
    Have a look at the 12 rules of Tcl syntax Commented Jul 9, 2020 at 12:56
  • C indexes from zero (just like Tcl); printing that array should have given 1.0 2.0 10.0 4.0 Commented Jul 9, 2020 at 14:18
  • Terminology: An array in TCL is, like in awk, an associative array/hash table. You're working with a list, which is more like a C array. Commented Jul 9, 2020 at 20:44
  • And what you should be seeing printed by that tcl code is 1.0 $a 3.0 4.0. What you say your C code prints is different from what should be seen too, though, so there's got to be more going on that you didn't share in the question. Commented Jul 9, 2020 at 21:08
  • thanks to everyone for you comments! Commented Jul 10, 2020 at 13:46

2 Answers 2

2

Curly brackets {} prevent substitution, try:

set b [list 1.0 $a 3.0 4.0]
Sign up to request clarification or add additional context in comments.

Comments

0

Though you've found that creating a list with substitutions is done via the list command, here's how to do the assignment to an element that is equivalent to b[2]=a; from C.

lset b 2 $a

1 Comment

This is technically not the answer to what you asked, but it does show what you might've been thinking about.

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.