How to unpack a list into variables in Tcl?

In Python, I can write something like:

my_list = [4, 7, "test"] a, b, c = my_list 

After that, a is 4 , b is 7 , and c is "test" due to the unpack operation on the last line. Can I do something like the last line in Tcl? To make this clearer, I need something like this:

 set my_list {4 7 test} setfromlist $mylist abc 

(Ie setfromlist will be the team I'm looking for.)

+4
source share
1 answer

Do you want lassign:

 % lassign wrong # args: should be "lassign list ?varName ...?" % lassign {1 2 3} abc % set a 1 % set b 2 % set c 3 

If you are using an older version of Tcl (which does not have lassign), you can use foreach to achieve the same result

 foreach {abc} {1 2 3} {break} 
+14
source

Source: https://habr.com/ru/post/1316561/


All Articles