variables - TCL multiple assignment (as in Perl or Ruby) -


in ruby or perl 1 can assign more variable using parentheses. example (in ruby):

(i,j) = [1,2] (k,m) = foo() #foo returns 2 element array 

can 1 accomplish same in tcl, in elegant way? mean know can do:

foreach varname { j } val { 1 2 } { set $varname $val } foreach varname { k m } val [ foo ] { set $varname $val } 

but hoping shorter/ less braces.

since tcl 8.5, can

lassign {1 2} j lassign [foo] k m 

note unintuitive left-to-right order of value sources -> variables. it's not unique design choice: e.g. scan , regexp use same convention. i'm 1 of find little less readable, once 1 has gotten used it's not problem.

if 1 needs ruby-like syntax, can arranged:

proc mset {vars vals} {     uplevel 1 [list lassign $vals {*}$vars] }  mset {i j} {1 2} mset {k m} [foo] 

before tcl 8.5 can use

foreach { j } { 1 2 } break foreach { k m } [ foo ] break 

which @ least has fewer braces in example.

documentation: break, foreach, lassign, list, proc, uplevel


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -