Makuza
Makuza

Reputation: 149

How do I concat a string to the end of an element of a list?

Suppose I have a list of variable length. I will just use length of 3 as an example

set inst_list [list a b c]

Now also suppose I have a variable:

set add_string "1"

I want to be able to add the $add_string variable to the last element in the list. Note that the list has a variable length and is not always 3.

The output I would want in the above example is:

a b c1

I know that if this were a fixed size list I could do something like

concat [lindex $inst_list 2]$add_string

but this would only give me "c1" not the full list with "c1" at the end. Also this does not account for variable list size of $inst_list.

Upvotes: 1

Views: 169

Answers (1)

glenn jackman
glenn jackman

Reputation: 246744

lset and string cat are appropriate here:

lset inst_list end [string cat [lindex $inst_list end] "1"]

string cat appears in Tcl v8.6

Upvotes: 4

Related Questions