lrange(n)

πŸ“¦
version 9.0.3 Β· 2 versions
, in alpine 3.23, alpine 3.24, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– 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 first is less than zero, it gets treated as if it were zero. (not me pretending I didn't see the negative. πŸ™ˆ)
  • if last is greater than or equal to the number of elements in the list, it gets treated as if it were end. iconic behaviour, honestly.
  • if first is greater than last, 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 first does not always produce the same result as lindex list first (although it often does for simple fields that aren't enclosed in braces). it does, however, produce exactly the same results as list [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