looking for what could be the "proc_of_for_loop"
puts $fp [proc_of_for_loop abc[$i] 5]
output should be like,
abc0
abc1
abc2
abc3
abc4
abc5
CodePudding user response:
The natural way of doing that in Tcl would be this:
proc n_range {varName from to body} {
upvar 1 $varName var
for {set var $from} {$var <= $to} {incr var} {
uplevel 1 $body
}
}
n_range i 0 5 {
puts $fp "abc$i"
}
The upvar
and uplevel
commands are a major part of the secret sauce of Tcl; they let you create your own control constructs in very little code.
CodePudding user response:
Here's the "substitute and collect" approach. Less general than Donal's solution, but closer to what's requested, and not significantly more complicated IMHO:
proc string_for {string count} {
for {set i 1} {$i <= count} {incr i} {
append result [subst $string]
}
return $result
}
puts $fp [string_for {abc$i\n} 5]