lpopTcl 9.1 only

Removes an element from the list in a variable and gives it.

lpop VAR ?INDEX ...?

Description

Removes the element at INDEX from the list in the variable VAR, and gives that element. The default INDEX is end, so lpop with no INDEX removes the last element. Each further INDEX goes one level down into a nested list, and one INDEX that is a list of indexes is the same path.

The index must be an element of the list: an empty list, or an index out of range, is an error, and VAR keeps its value. VAR must have a value.

Arguments

ArgumentTypeDefaultDescription
VARvariable namerequiredThe variable with the list.
INDEXindex, or a list of indexesendThe place of the element, one level of the path each.

Returns

The element that was taken out.

Examples

% set l {a b c}
> list [lpop l] $l
c {a b}

% set l {a b c}
> list [lpop l 0] $l
a {b c}

% set l {a b c}
> list [lpop l end-1] $l
b {a c}

% set l {{a b} c}
> list [lpop l 0 0] $l
a {b c}

Errors

% set l {}
> lpop l
index "end" out of range
-errorcode TCL VALUE INDEX OUTOFRANGE

% set l {a b}
> lpop l 5
index "5" out of range
-errorcode TCL VALUE INDEX OUTOFRANGE

% unset -nocomplain nv
> lpop nv
can't read "nv": no such variable
-errorcode NONE

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 only.
Since
ztcl 1.0.0-beta.1

See also