lrange(3tcl)
π lrange β bestie edition π
okay so you know how Tcl lists are literally the moment, and sometimes you just want a slice of one? not the whole thing, just a lil adjacent chunk? yeah. that's this. it's lrange, it's part of the Tcl built-in commands crew, and it's about to hand you one or more adjacent elements from a list. slay. β¨
πΈ SYNOPSIS πΈ
lrange list first last
β¨ DESCRIPTION β¨
so. list has to be a valid Tcl list β no cap, that's the vibe check. once it passes, this command returns a brand new list made up of the elements from first through last, inclusive. both ends, both included, we don't do exclusion here. π«Ά
now the tea on first and last: those index values get interpreted exactly the same way index values work for the command string index. so you get simple index arithmetic and you get indices relative to the end of the list. math girlies rise. π
and here's the edge-case lore:
- if
firstis less than zero, it's treated as if it were zero - if
lastis greater than or equal to the number of elements in the list, it's treated as if it wereend - if
firstis greater thanlast, you get an empty string back. nothing. void. the audacity π
okay this next bit is important so pay attention bestie π βlrange list first firstdoes not always produce the same result aslindex list first. it often does, but only for simple fields that aren't enclosed in braces. what it does produce, exactly, is the same results aslist [lindex list first]. remember that. it's on the exam. π
π EXAMPLES π
selecting the first two elements β classic, iconic:
% lrange {a b c d e} 0 1
a b
selecting the last three elements β end relative indexing ate that fr fr:
% lrange {a b c d e} end-2 end
c d e
selecting everything except the first and last element β the "no thank you to the edges" special:
% lrange {a b c d e} 1 end-1
b c d
and now the one that catches everybody slipping: selecting a single element with lrange is not the same as doing so with lindex. look at this glow-up difference:
% set var {some {elements to} select}
some {elements to} select
% lindex $var 1
elements to
% lrange $var 1 1
{elements to}
see how lrange kept the braces?? yeah. it returns a list, and a list with one element needs its braces. that's just how it is. π¦
π SEE ALSO π
go read these next, they're all in the same friend group:
list(3tcl)lappend(3tcl)lassign(3tcl)ledit(3tcl)lindex(3tcl)linsert(3tcl)llength(3tcl)lmap(3tcl)lpop(3tcl)lremove(3tcl)lrepeat(3tcl)lreplace(3tcl)lreverse(3tcl)lsearch(3tcl)lseq(3tcl)lset(3tcl)lsort(3tcl)string(3tcl)
π KEYWORDS π
element, list, range, sublist
mwah π that's the whole page. this one came from the Tcl manual set, version 7.4, dated back in the day, and it lives in section 3tcl under the name lrange. go forth and slice those lists, bestie. you got this ππβ¨
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- 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