set a {1 3}
set b {}
set c_num {2 10 15 30}
lappend a $c_num
set d [concat $a $b]
puts $d
I want to combine this a and b set but the values should be separated. from above code I get output as 1 3 {2 10 15 30} instead I want output as 1 3 2 10 15 30
if there any solution please guide.
To change $d
's value from 1 3 {2 10 15 30}
to 1 3 2 10 15 30
, there are a few ways.
One seemingly simple way is using join
join $d
--> 1 3 2 10 15 30
...but this join
is actually a string command. To illustrate further, add a second argument to join
and see how often the joining character appears.
join $d "#"
--> 1#3#2 10 15 30
Because Tcl doesn't have strict typing, a string can often be used as an argument to a list command (like lindex
or llength
). It's always more memory efficient to avoid converting values from strings to lists.
The concat
command is a list command to form a new list from its arguments. Using the {*}
operator converts the elements of a list into individual arguments.
concat {*}$d
--> 1 3 2 10 15 30
The best solution would be to avoid needing to use join
or concat
at all. Append multiple arguments to your list with the {*}
operator instead of appending a list to a list.