lpop(3tcl)
β¨ lpop β bestie edition β¨
okay so you know how lindex gives you a lil peek at an element but leaves the list exactly as it was? yeah. lpop is not that. lpop is lindex with main character energy β it grabs the element AND yeets it out of the list. from lpop in the Tcl Built-In Commands manual page (yes she's a 3tcl girlie) π
πΈ NAME πΈ
lpop β get an element out of a list and remove it. steal it. take it home. gone.
π SYNOPSIS π
lpop varName ?index ...?
π DESCRIPTION π
So here's the vibe. lpop takes one parameter, varName, and it's like "okay bestie, I'm treating you as the name of a variable that holds a Tcl list". It also accepts one or more indices pointing into that list. And if you're like "indices? don't know her" and pass none at all? it just defaults to "end". no thoughts, head empty, popping the last one by default. iconique.
When you hand it a single index, lpop goes to the index'th element in that list, removes it from the list, and returns the element to you. both things. it's giving multitasking. π«Ά
β οΈ but listen β ifindexis negative, or greater than or equal to the number of elements in the list insidevarName, an error happens. do not test her. π
The way each simple index value gets interpreted is exactly the same as for the command string index β so yes, simple index arithmetic is supported, and yes, indices relative to the end of the list work too. end, end-1, all of that. slay.
Now the fun part. If you supply additional index arguments, then each argument gets used in turn to address an element inside a sublist picked out by the previous indexing operation. so you can remove elements nested way down in sublists, similar to how lindex and lset do it. for example:
lpop a 1 2
gets and removes element 2 of sublist 1. deep dive. π¦
π EXAMPLES π
In each of these examples, the initial value of x is:
set x [list [list a b c] [list d e f] [list g h i]]
β {a b c} {d e f} {g h i}
The indicated value becomes the new value of x β except in the last case, which is an error, and which leaves the value of x untouched. (she's messy but she's not THAT messy. your list is safe.)
lpop x 0
β {d e f} {g h i}
lpop x 2
β {a b c} {d e f}
lpop x end
β {a b c} {d e f}
lpop x end-1
β {a b c} {g h i}
lpop x 2 1
β {a b c} {d e f} {g i}
lpop x 2 3 j
β list index out of range
In the following examples, the initial value of x is:
set x [list [list [list a b] [list c d]] \
[list [list e f] [list g h]]]
β {{a b} {c d}} {{e f} {g h}}
And the indicated value becomes the new value of x:
lpop x 1 1 0
β {{a b} {c d}} {{e f} h}
look at that nesting get un-nested. we love a glow up. β¨
π SEE ALSO π
go read these too, they're all in the same friend group:
list(3tcl)lappend(3tcl)lassign(3tcl)ledit(3tcl)lindex(3tcl)linsert(3tcl)llength(3tcl)lmap(3tcl)lrange(3tcl)lremove(3tcl)lrepeat(3tcl)lreplace(3tcl)lreverse(3tcl)lsearch(3tcl)lseq(3tcl)lset(3tcl)lsort(3tcl)string(3tcl)
π₯ KEYWORDS π₯
element, index, list, remove, pop, stack, queue
stack AND queue?? she contains multitudes, periodt. π
and that's the whole page bestie, no cap. this was Tcl, version 9.0, lpop(3tcl) β mwah, go pop something. ππ§
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2018 Peter Spjuth. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms