See Tcl shell code & results below:
% foreach a { 1 2 3 4 5 } { puts $a }
1
2
3
4
5
% puts $a
5
It would appear that variable a stays in the memory... In Perl, for example one can use:
foreach my $a
And have $a exist just during the looping. in Ruby, one can use closures:
[1,2,3,4,5].each{|a| puts a.to_s() }
In order to achieve the same.
Is there an elegant way to this in Tcl?
Thanks
unsetcommand to achieve the same after theforeachloop.foreachcommand's behavior withrenameand do theunsetoperation for you.