linsert(3tcl)

πŸ“¦
version 9.0.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– 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 πŸ¦‹


✨ 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