lseq(n) Tcl Built-In Commands lseq(n)

```

```

# [NAME](#NAME)

lseq - Build a numeric sequence returned as a list

# [SYNOPSIS](#SYNOPSIS)

```
lseq start ?(..|to)? end ??by? step?
lseq start count count ??by? step?
lseq count ?by step?
```

```

```

# [DESCRIPTION](#DESCRIPTION)

The **lseq** command creates a sequence of numeric values, which may be either wide integers or doubles, using the given parameters *start*, *end*, and *step*. The *operation* argument "**..**" or "**to**" defines the range. The "**count**" option is used to define a count of the number of elements in the list. A short form use of the command, with a single count value, will create a range from 0 to *count*-1.

The **lseq** command can produce both increasing and decreasing sequences. When both *start* and *end* are provided without a *step* value, then if *start* &lt;= *end*, the sequence will be increasing and if *start* &gt; *end* it will be decreasing. If a *step* value is included, it's sign should agree with the direction of the sequence (descending → negative and ascending → positive), otherwise the list will have a length of 0. 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. **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](#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](#SEE_ALSO)

[foreach(n)](/man/os/tcl/foreach.n), [list(n)](/man/os/tcl/list.n), [lappend(n)](/man/os/tcl/lappend.n), [lassign(n)](/man/os/tcl/lassign.n), [lindex(n)](/man/os/tcl/lindex.n), [linsert(n)](/man/os/tcl/linsert.n), [llength(n)](/man/os/tcl/llength.n), [lmap(n)](/man/os/tcl/lmap.n), [lpop(n)](/man/os/tcl/lpop.n), [lrange(n)](/man/os/tcl/lrange.n), [lremove(n)](/man/os/tcl/lremove.n), [lreplace(n)](/man/os/tcl/lreplace.n), [lreverse(n)](/man/os/tcl/lreverse.n), [lsearch(n)](/man/os/tcl/lsearch.n), [lset(n)](/man/os/tcl/lset.n), [lsort(n)](/man/os/tcl/lsort.n)

# [KEYWORDS](#KEYWORDS)

element, index, list

9.0 Tcl

---

Document: lseq(n) from tcl 9.1.0-r2 (Chainguard OS)
Source: https://downloads.sourceforge.net/project/tcl/Tcl/9.1.0/tcl9.1.0-src.tar.gz
