linsert(n)

πŸ’– NAME πŸ’–

linsert β€” insert elements into a list. That's the whole gig, and she does it flawlessly every single time. ✨


πŸŽ€ SYNOPSIS πŸŽ€

linsert list index ?element element ...?

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so picture this bestie: you've got a list, and you want to slide some new stuff into it without messing up the original. linsert says "say no more" and hands you back a whole new list β€” it takes your list and inserts every single one of the element arguments just before the index'th element of list. πŸ”₯

And here's the thing: each element argument becomes its own separate element of the new list. No merging, no gluing them together, they are all individuals with main character energy. πŸ’…

Now the index rules, because this is where it gets juicy:

  • If index is less than or equal to zero, the new elements get inserted at the beginning of the list 🌸
  • If index is greater than or equal to the length of list, it's as if you'd written end πŸ‘‘

And just like with string index, the index value supports both simple index arithmetic and end-relative indexing. Do the math, count from the end, whatever feels right. ✨

Subject to the restrictions that indices must refer to locations inside the list, and that the elements will always be inserted in order, insertions are done so that: - 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

🌈 EXAMPLE 🌈

Putting some values into a list, first indexing from the start and then indexing from the end, and then chaining them all together β€” obsessed with the chaining ngl:

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]

πŸ‘€ SEE ALSO πŸ‘€


πŸ’œ KEYWORDS πŸ’œ

element, insert, list


Okay that's the whole page, you ate that. This one's from the Tcl Built-In Commands manual, version 8.2 β€” the footer keeps it short and sweet with just Tcl and 8.2, no date on this one, so don't let anyone tell you otherwise. Go forth and insert things, bestie 🫢✨

πŸ“¦ where this page comes from