Created
May 11, 2020 10:53
-
-
Save sebres/8ef0bb82eb39d119cb2f22efe5ee3ed5 to your computer and use it in GitHub Desktop.
lpop -- definition for lpop command (compatible to TIP 523 implementation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if {[namespace which -command ::lpop] eq {}} { | |
proc ::lpop {listvar {index end} args} { | |
upvar $listvar l | |
if {![info exists l]} { | |
return -code error "can't read \"$listvar\": no such variable" | |
} | |
if {![llength $args]} { | |
if {$index eq "end"} { | |
if {![llength $l]} { | |
return -code error -errorcode {TCL VALUE INDEX OUTOFRANGE} "index \"$index\" out of range" | |
} | |
} elseif {[string is integer -strict $index]} { | |
if {$index >= [llength $l] || $index < 0} { | |
return -code error -errorcode {TCL VALUE INDEX OUTOFRANGE} "index \"$index\" out of range" | |
} | |
} else { | |
set i [expr [regsub {\mend\M} $index [expr {[llength $l]-1}]]] | |
if {$i >= [llength $l] || $i < 0} { | |
return -code error -errorcode {TCL VALUE INDEX OUTOFRANGE} "index \"$index\" out of range" | |
} | |
} | |
set v [lindex $l $index] | |
set l [lreplace $l [set l $index] $index] | |
} else { | |
set v [lindex $l $index {*}$args] | |
set i [lrange $args 0 end-1] | |
set l2 [lindex $l $index {*}$i] | |
lset l $index {*}$i [lreplace $l2 [lindex $args end] [lindex $args end]] | |
} | |
set v | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment