Skip to content
Tcl Source Code Artifact [d94ab39af0]
Login

Artifact [d94ab39af0]

Artifact d94ab39af06974d38771827d68917ae8f1fae929e79641bad29b4e3a8a13f0b8:



CommandName: lpop ManualSection: n Version: 9.0 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:

Name

lpop - Get and remove an element in a list

Synopsis

::: {.synopsis} ::: [lpop]{.cmd} [varName]{.arg} [?index]{.arg} [...?]{.arg} :::

Description

The fBlpopfR command accepts a parameter, fIvarNamefR, which it interprets as the name of a variable containing a Tcl list. It also accepts one or more fIindicesfR into the list. If no indices are presented, it defaults to "fBendfR".

When presented with a single index, the fBlpopfR command addresses the fIindexfR'th element in it, removes if from the list and returns the element.

If fIindexfR is negative or greater or equal than the number of elements in the list in the variable called fIvarNamefR, an error occurs.

The interpretation of each simple fIindexfR value is the same as for the command fBstring indexfR, supporting simple index arithmetic and indices relative to the end of the list.

If additional fIindexfR arguments are supplied, then each argument is used in turn to address an element within a sublist designated by the previous indexing operation, allowing the script to remove elements in sublists, similar to fBlindexfR and fBlsetfR. The command,

lpop a 1 2

gets and removes element 2 of sublist 1.

Examples

In each of these examples, the initial value of fIxfR 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 fIxfR (except in the last case, which is an error which leaves the value of fIxfR unchanged.)

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 fIxfR 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}}

The indicated value becomes the new value of fIxfR.

lpop x 1 1 0
      \(-> {{a b} {c d}} {{e f} h}