tags:

views:

46

answers:

1

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" because of the unpack operation in the last line. Can I do something like the last line in Tcl? To make it more clear, I want something like that:

set my_list {4 7 test}
setfromlist $mylist a b c

(I.e. setfromlist would be the command I'm looking for.)

+9  A: 

You want lassign:

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

If you're using an older version of Tcl (that doesn't have lassign), you can use foreach to achieve the same result

foreach {a b c} {1 2 3} {break}
RHSeeger
lassign comes standard on Tcl 8.5. If you are using Tcl 8.4: package require Tclx
Hai Vu
Or, if you don't want all of Tclx, you can just write a version of lassign that uses foreach to achieve the same functionality (foreach was commonly used for this purpose prior to the addition of lassign)
RHSeeger