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

I am wondering if we should recommend new users to use the "lset" command to create a list instead of using "set foo [list]".

Any opinions, pro, cons?

Collapse
Posted by Anett Szabo on
Using lset gives you the opportunity to insert elements at a given position and work with positions within a list in general.
Collapse
Posted by Anett Szabo on
For example you can use:

lset x end-1 j => {a b c} j {g h i}

Collapse
Posted by Dave Bauer on
lset doesn't seem to work for initializing a list.

% lset foo [list]
can't read "foo": no such variable

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.