lrange(n)
π lrange(n) β the Tcl Built-In Commands glow-up π
okay bestie, welcome to the lrange page, aka the one where Tcl hands you a little slice of a list and you're like "wait that's it? that's adorable." no cap, this one's a cutie. π
β¨ NAME β¨
lrange β grab one or more elements that are sitting right next to each other in a list. like a lil friend group. adjacent only. no gaps. π
β¨ SYNOPSIS β¨
lrange list first last
three args, that's the whole vibe. π¦
β¨ DESCRIPTION β¨
so. list has to be a valid Tcl list β that's the vibe check, don't skip it. π
this command returns a new list made of the elements from first through last, inclusive (yes bestie, both ends get invited to the party π«Ά).
the index values first and last get read exactly the same way as index values for the string index command β so you get simple index arithmetic and indices relative to the end of the list. you know, the end-2 type of slay. β¨
now the edge cases, and honestly these are the main character moments:
- if
firstis less than zero, it gets treated as if it were zero. (not me pretending I didn't see the negative. π) - if
lastis greater than or equal to the number of elements in the list, it gets treated as if it wereend. iconic behaviour, honestly. - if
firstis greater thanlast, you get back an empty string. it's giving "we have nothing to say to each other." π
β οΈ and here's the tea, the one that gets people every time:lrange list first firstdoes not always produce the same result aslindex list first(although it often does for simple fields that aren't enclosed in braces). it does, however, produce exactly the same results aslist [lindex list first]. so likeβ¦ brace-aware, bestie. it's different. remember it. π
β¨ EXAMPLES β¨
selecting the first two elements:
% lrange {a b c d e} 0 1
a b
selecting the last three elements:
% lrange {a b c d e} end-2 end
c d e
selecting everything except the first and last element:
% lrange {a b c d e} 1 end-1
b c d
selecting a single element with lrange is NOT the same as doing so with lindex β here's the receipts, look at this:
% set var {some {elements to} select}
some {elements to} select
% lindex $var 1
elements to
% lrange $var 1 1
{elements to}
see?? π lindex hands you the raw guts. lrange hands you the guts wrapped back up as a list with braces. both valid, just different aesthetics. periodt. π
β¨ SEE ALSO β¨
go be nosy, bestie, the whole Tcl list family is right here:
list(n), lappend(n), lassign(n), ledit(n), lindex(n), linsert(n), llength(n), lmap(n), lpop(n), lremove(n), lrepeat(n), lreplace(n), lreverse(n), lsearch(n), lseq(n), lset(n), lsort(n), string(n)
β¨ KEYWORDS β¨
element, list, range, sublist πΈ
okay that's the whole page, you ate it, I'm obsessed. π₯° this one's from the Tcl Built-In Commands, living in Tcl 7.4 β so yeah, lowkey vintage, highkey still useful. mwah, go slice a list. ππβ
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 2001 Kevin B. Kenny <kennykb@acm.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms