Mike Winfield
Mike Winfield

Reputation: 23

How do I concatenate 2 variable names into 1 name in TCL?

I've got a group of variables that are lists of bytes that I'm trying to search through.

set foo0 [list 0 1 2]
set foo1 [list 3 4 5]
set foo2 [list 6 7 8]
set fooCount 3

for { set z 0 } { $z<$fooCount} { incr z } { 
    tbf str "foo$z: foo$z"
}

I'd like this to print out:

foo0: 0 1 2
foo1: 3 4 5
foo2: 6 7 8

But I get:

foo0: foo0
foo1: foo1
foo2: foo2

I tried changing my print line to

tbf str "foo$z: $foo$z"

But then I get an error that there is no variable foo. I've also tried copying all of the foo variables into a fooList, but again I don't seem to be getting the contents.

Upvotes: 2

Views: 1295

Answers (2)

glenn jackman
glenn jackman

Reputation: 246807

Using arrays, it's easy too:

set foo(0) [list 0 1 2]
set foo(1) [list 3 4 5]
set foo(2) [list 6 7 8]
set fooCount 3

for { set z 0 } { $z<$fooCount} { incr z } { 
    tbf str "foo($z): $foo($z)"
}

Upvotes: 0

Hai Vu
Hai Vu

Reputation: 40723

Try this:

tbf str "foo$z: [set foo$z]"

The set command returns the value of that token, which is foo0, foo1 and so on.

Upvotes: 8

Related Questions