lseq(3tcl)
π lseq β bestie, it's the Tcl Built-In Commands page, 3tcl section, and we're building sequences π
β¨ NAME β¨
lseq β build a numeric sequence and get it back as a list π
π¦ SYNOPSIS π¦
lseq start ?(..|to)? end ??by? step?
lseq start count count ??by? step?
lseq count ?by step?
πΈ DESCRIPTION πΈ
okay so the lseq command makes a sequence of numeric values β and those values can be wide integers or doubles, your call β using the params start, end, and step. The operation argument .. or to is what defines the range, like it's the little official handshake for "yeah this is a range". The count option is a whole different thing: it defines a count of the number of elements in the list. And there's a short form, where you just hand it a single count value, and boom: a range from 0 to count-1. π«Ά
lseq can do both increasing and decreasing sequences, she's versatile like that. When both start and end show up with no step value, then if start <= end, the sequence will be increasing, and if start > end it will be decreasing. But if a step value is included, its sign should agree with the direction of the sequence (descending β negative, ascending β positive), otherwise the list will have a length of 0. like, an empty list, a little nothing-burger. For example:
# increasing:
% lseq 1 to 5
β 1 2 3 4 5
# decreasing:
% lseq 5 to 1
β 5 4 3 2 1
# doubles:
% lseq 0 0.5 by 0.1
β 0.0 0.1 0.2 0.3 0.4 0.5
# decreasing, step with wrong sign:
% lseq 6 to 1 by 2
β {}
# step of 0,
% lseq 3 to 9 by 0
β 3
start defines the initial value and end defines the limit β not necessarily the last value, bestie, keep that in mind. lseq produces a list with count elements, always, even if the step value is 0. And if count is not supplied, it is computed as:
count = int( ( (end - start) / step ) + 1 )
π EXAMPLES π
lseq 3
β 0 1 2
lseq 3 0
β 3 2 1 0
lseq 10 .. 1 by -2
β 10 8 6 4 2
set l [lseq 0 -5]
β 0 -1 -2 -3 -4 -5
lseq 1 count 5 by 0
β 1 1 1 1 1
foreach i [lseq [llength $l]] {
puts l($i)=[lindex $l $i]
}
β l(0)=0
β l(1)=-1
β l(2)=-2
β l(3)=-3
β l(4)=-4
β l(5)=-5
foreach i [lseq {[llength $l]-1} 0] {
puts l($i)=[lindex $l $i]
}
β l(5)=-5
β l(4)=-4
β l(3)=-3
β l(2)=-2
β l(1)=-1
β l(0)=0
set i 17
β 17
if {$i in [lseq 0 50]} { # equivalent to: (0 <= $i && $i <= 50)
puts "Ok"
} else {
puts "outside :("
} β Ok
set sqrs [lmap i [lseq 1 10] { expr {$i*$i} }]
β 1 4 9 16 25 36 49 64 81 100
π SEE ALSO π
foreach(3tcl), list(3tcl), lappend(3tcl), lassign(3tcl), lindex(3tcl), linsert(3tcl), llength(3tcl), lmap(3tcl), lpop(3tcl), lrange(3tcl), lremove(3tcl), lreplace(3tcl), lreverse(3tcl), lsearch(3tcl), lset(3tcl), lsort(3tcl)
the whole list squad fr fr, go read all of them, they're iconic π
β KEYWORDS β
element, index, list
okay and that's the whole vibe check β this page is straight outta Tcl 9.0, lseq(3tcl), and it literally ate π
β¨ no cap, go build some sequences bestie, you got this π¦ππΈπ
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2022 Eric Taylor. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms