Version 7 of linsert

Updated 2004-02-27 13:53:57

linsert - insert elements into a list

http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/linsert.htm


 linsert list index element1 ?element2 element3 ...?

A user asked why

    linsert $list 0 ..

did not result in .. being inserted into list.

Jesper Blommaskog ([email protected]) replied:

When doing list operations other than lappend, you must save the returned value. This applies to list, lindex, lrange, and lreplace at least.

In this example, you would perhaps want to do something like:

    set list [ linsert $list 0 .. ]

TFW Feb 20, 2004 - Inplace inserts

Often time we have a long list we want to insert "in-place", that is without copying the list to a new altered list. Using the K combinator it is quite easy. (Note 8.5 is needed for the {expand} command)

 proc lipinsert {_list index args} {
    upvar $_list list
    set list [linsert [K $list [set list ""]] $index {expand}$args]
 }
 > set A [list 1 2 3]
 1 2 3
 > lipinsert A 1 a b c
 1 a b c 2 3

See also list, lappend, llength, lrange, lreplace, lsearch, lsort .


Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command