Updated 2012-09-25 17:01:13 by LkpPo

Command in the TclX package.
lvarpop var ?indexExpr? ?string?

The lvarpop command pops (deletes) the element indexed by the expression indexExpr from the list contained in the variable var. If indexExpr is omitted, then 0 is assumed. If string is specified, then the deleted element is replaced by string. The replaced or deleted element is returned. Thus
  lvarpop argv 0

returns the first element of argv, setting argv to contain the remainder of the string.

If the expression indexExpr starts with the string "end", then end is replaced with the index of the last element in the list. If the expression starts with "len", then len is replaced with the length of the list.

See also lvarpush.

A quick Tcl-only lvarpop, if you don't want to include all of Tclx in your starpack or starkit:
 proc lvarpop {upVar {index 0}} {
  upvar $upVar list;
  if {![info exists list]} { return "-1" }
  set top [lindex $list $index];
  set list [concat [lrange $list 0 [expr $index - 1]] [lrange $list [expr $index +1] end]]
  return $top;

Sourced from the Wiki somewhere ages ago or perhaps Usenet.

Lars H: Simplified implementation, using lreplace instead of lrange&concat:
 proc lvarpop {upVar {index 0}} {
    upvar 1 $upVar list
    if {![info exists list]} { return "-1" }
    set top [lindex $list $index]
    set list [lreplace $list $index $index]
    return $top

Unlike the above, this supports end-relative indices. It will however throw an error if the index is out-of-range.

Neither proc supports general expressions or len-relative indices, as described in the manpage text above. Nor do they support the third argument of TclX's lvarpop.