Version 3 of lset

Updated 2002-10-22 08:02:50

http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/lset.htm will eventually have the formal man page for lset.


Kevin Kenny writes on news:comp.lang.tcl.announce :

 Subject: TIP's 22, 33, 45 - extended [list] functionality - now final.
 Date: 18 Nov 2001 11:30:33 -0800
 Message-ID: <[email protected]>

Jeff Hobbs today committed to the CVS HEAD at Sourceforge the changes described in TIP's 22 (Multiple Index Arguments to lindex), 33 (Add 'lset' Command to Assign to List Elements), and 45 (Empty index lists for lindex and lset)

These changes augment the lindex command so that it can extract elements from sublists, for example:

    [lindex {{a b c} {d e f} {g h i}} 1 1] => e

They also implement an lset command that may be used to change individual elements within lists and sublists. Taken together, these commands can be used to treat lists as if they were linear arrays. For instance, the following procedure might be used to reverse the order of elements in a list.

    proc reverse { list } {
        set i 0
        set j [expr { [llength $list] - 1 }]
        while { $j > $i } {
            set temp [lindex $list $i]
            lset list $i [lindex $list $j]
            lset list $j $temp
            incr i
            incr j -1
        }
        return $list
    }

Updated documentation for the commands is available in the 'doc/' subdirectory in the source tree. The original proposals may be found at

    http://www.purl.org/tcl/tip/22.htm 
    http://www.purl.org/tcl/tip/33.htm 
    http://www.purl.org/tcl/tip/45.htm





[Explain why lset is A Good Thing.]


I have been trying the new command 'lset' and it is a nice addition to the core. However, it does not permmit to create new elements in the list. Why not?

Today, there is not a simple method in TCL to convert the list:

  {a a} {b b} {c c}

into:

  {a a} {b b b} {c c}

it would be nice to do:

   lset list 1 2 b

There are many mathematical algorithms that need to fill a list, but not necessarily from the beginning. So, a command that filled position n of the list and filled with "" the elements from 0 to n-1 would be a good adition to the lset command.

For me, the best option is just change the lset especification to permmit to do so. Maybe other people would prefer an additional option:

   lset -create list 1 1 element

I would like to collect here other opinions