Storing raw Tcl values
In some cases, you may decide not to use any additional packages, yet you will need to save some data for future usage. In Tcl, it is easy to store data structures like lists or dictionaries in flat text file.
First, let's demonstrate how to write to and read from a list:
set book1 [list {Learning Nagios 3.0} 978-1-847195-18-0] set book2 [list {Tcl Network Programming} 978-1-849510-96-7] set books [list $book1 $book2] puts [lindex [lindex $books end] 0] set channel [open data.txt w] puts $channel $books close $channel set channel [open data.txt r] set books [read $channel] close $channel puts [lindex [lindex $books end] 0]
books
variable is a list of two lists, each one containing the title and ISBN number, respectively. Before saving, we print out the name of the second book. Then, the $books
is written to data.txt
file, and in the next lines its contents are read and stored again into the books
variable. It is the unique Tcl feature that allows treating lists as...