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.
Comments
Post a Comment