In TCL, how to append different content into a single file using the for
loop or foreach
loop?
DESCRIPTION. Append all of the value arguments to the current value of variable varName. If varName doesn't exist, it is given a value equal to the concatenation of all the value arguments. This command provides an efficient way to build up long variables incrementally.
Here's a section of the man page for Tcl's open command: r Open the file for reading only; the file must already exist. This is the default value if access is not specified. r+ Open the file for both reading and writing; the file must already exist.
Tcl supports file handling with the help of the built in commands open, read, puts, gets, and close.
Did you mean something like that?
set fo [open file a]
foreach different_content {"text 1" "text two" "something else" "some content"} {
puts $fo $different_content
}
close $fo
You open file file
in mode a
(append) and write to the file descriptor ($fo
in the example).
Update: If you want to append variable contents, you have to change the script to:
set fo [open file a]
foreach different_content [list $data1 $data2 $data3 $data4] {
puts $fo $different_content
}
close $fo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With