linsert(3tcl)
π linsert β the Tcl Built-In Commands glow-up π
okay bestie, gather round, because we're about to talk about linsert, aka the little Tcl command that slides new elements into a list like it's tucking a note into someone's locker. π
β¨ NAME β¨
linsert β insert elements into a list. that's it. that's the whole job. she does one thing and she does it flawlessly. π
β¨ SYNOPSIS β¨
linsert list index ?element element ...?
β¨ DESCRIPTION β¨
so here's the vibe π β this command makes a brand new list out of list by inserting all of those element arguments just before the index'th element of list. she does not mutate the original. she creates. main character energy, honestly.
every single element argument becomes its own separate element of the new list. no smushing them together, no gluing, each one gets its own little seat at the table. πͺ
now the index math, bestie, pay attention because this is where people get caught slipping:
- if index is less than or equal to zero, the new elements go at the beginning of the list. front row seats. π
- if index is greater than or equal to the length of list, it's exactly as if you'd written
end. it just clamps, no drama. π«Ά
and just like with string index, the index value supports both simple index arithmetic and end-relative indexing. so yes you can do end-1, end-2, go off. β¨
okay now the spicy fine print πΆοΈ β subject to these restrictions:
- indices must refer to locations inside the list (no wandering off, she's not gonna chase you)
- the elements will always be inserted in order
given those, here's the rule: when index is start-relative, the first element will be at that index in the resulting list. when index is end-relative, the last element will be at that index in the resulting list. so like, the anchor point shifts depending on which way you're counting. iykyk. π
β¨ EXAMPLE β¨
putting some values into a list, first indexing from the start and then indexing from the end, and then chaining them together β look at this absolute serve:
set oldList {the fox jumps over the dog}
set midList [linsert $oldList 1 quick]
set newList [linsert $midList end-1 lazy]
# The old lists still exist though...
set newerList [linsert [linsert $oldList end-1 quick] 1 lazy]
π«Ά notice that comment, bestie β the old lists still exist. linsert hands you back a fresh list and leaves the original completely untouched. she's not messy. she's not destructive. she's a builder. π
β¨ SEE ALSO β¨
her whole friend group, fr fr π¦
list(3tcl)lappend(3tcl)lassign(3tcl)ledit(3tcl)lindex(3tcl)llength(3tcl)lmap(3tcl)lpop(3tcl)lrange(3tcl)lremove(3tcl)lrepeat(3tcl)lreplace(3tcl)lreverse(3tcl)lsearch(3tcl)lseq(3tcl)lset(3tcl)lsort(3tcl)string(3tcl)
β¨ KEYWORDS β¨
element, insert, list π
aaaand that's the whole page, bestie!! π this was Tcl 8.2, part of the Tcl Built-In Commands lineup, and she's been serving since forever. go forth and insert things. slay. π β¨π
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 2001 Kevin B. Kenny <kennykb@acm.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms