Tcl foreach Command
last modified April 3, 2025
The Tcl foreach
command is used to iterate over lists and arrays.
It's a powerful looping construct that processes each element in a sequence.
The command can handle multiple lists simultaneously.
Basic Definition
The foreach
command executes a script for each element in one or
more lists. It assigns each element to a variable and runs the script body.
Syntax: foreach varList list ?varList list ...? script
. It takes
variable names, lists to iterate over, and a script to execute for each element.
Basic List Iteration
This example shows the simplest usage of foreach
to iterate over
a single list.
set colors {red green blue yellow} foreach color $colors { puts "Color: $color" }
This creates a list of colors and iterates over each element. The current
color is assigned to the color
variable in each iteration.
Multiple List Variables
foreach
can iterate over multiple lists simultaneously by using
multiple variable names.
set names {Alice Bob Charlie} set ages {25 30 35} foreach name $names age $ages { puts "$name is $age years old" }
This pairs elements from two lists together. The command takes elements from both lists in parallel during each iteration.
Nested Lists
foreach
can handle nested lists by using multiple loop variables.
set points {{1 2} {3 4} {5 6}} foreach point $points { lassign $point x y puts "Point at ($x, $y)" }
This demonstrates processing nested lists. Each point is a sublist that gets
split into x and y coordinates using lassign
.
Multiple Variables per List
You can specify multiple variables to consume multiple elements from a list in each iteration.
set numbers {1 2 3 4 5 6 7 8} foreach {a b} $numbers { puts "Pair: $a and $b" }
This processes the list two elements at a time. The loop takes two elements per iteration until the list is exhausted.
Iterating Over Arrays
To iterate over arrays, we first need to get the array keys using array names
.
array set user { name "John Doe" age 30 city "New York" } foreach key [array names user] { puts "$key: $user($key)" }
This shows how to iterate over an array's keys and values. We first get all keys then access each value using array syntax.
Break and Continue
foreach
supports break
and continue
commands for loop control.
set nums {1 2 3 4 5 6 7 8 9 10} foreach num $nums { if {$num == 5} {continue} if {$num == 8} {break} puts "Number: $num" }
This demonstrates loop control. It skips number 5 with continue
and exits the loop at number 8 with break
.
Best Practices
- Variable names: Use meaningful names for iteration variables.
- Large lists: Consider performance with very large lists.
- List modification: Avoid modifying the list during iteration.
- Error handling: Check list validity before iteration.
- Scope: Be aware of variable scope in the loop body.
This tutorial covered the Tcl foreach
command with practical
examples showing its usage in different scenarios.
Author
List all Tcl Tutorials.