Forum OpenACS Development: Re: set foo [list bar] vs. lset foo {bar}

Collapse
Posted by Malte Sussdorff on
Silly me. It works only on existing lists. Forget me asking
Collapse
Posted by Tom Jackson on
The value of lset is in working with single elements of nested lists, and probably more efficient than lreplace for replacing single elements of a simple list:
% lset myList {a b c d}
a b c d
% lset myList "" {a b c d}
a b c d
% lset myList 1 r
a r c d
% puts $myList
a r c d
% lset myList 1 b
a b c d
% lreplace $myList 1 1 r
a r c d
% puts $myList
a b c d
% set myList [lreplace $myList 1 1 r]
a r c d
% puts $myList
a r c d
%%%%
% set myOtherList [list $myList {h  i}]
{a r c d} {h  i}
% lset myOtherList {0 1} b
{a b c d} {h  i}

So you can use lset as in lines 1 and 2 with an empty index to set the initial list.