# Tcl Learning Notes

Programming | 09 October 2017

In this page, you will find the commonly used syntax and how to’s that is specific to Tcl programming language. If you know what Tcl is, but wanted to quickly refer to Tcl syntax, then you might find this page useful. I will keep on updating this article. So, kindly bookmark this page if you find it useful :)

### Procscode

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # a simple proc with two arguments and a return statement proc addition {a b} { set c [expr $a +$b] return $c } puts "[addition 5 4]" # proc with multiple arguments # "args" as the third argument holds all additional values supplied proc more_args {a b args} { puts$a puts $b puts$args } # prints # 5 # 6 # 90 60 10 more_args 5 6 90 60 10

### File handlingcode

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 # opens a file to write set fp [open "sample_text_file" w] # writes data to the file puts $fp "Hello World" # close the file after writing (else data won't be saved) close$fp # open a file to read set fp [open "sample_text_file" r] # read the content of the file set fp_data [read $fp] # split by newline set lines [split$fp_data "\n"] # loop through each line in the file foreach line \$lines { # do some processing }

### Utilscode

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # execute shell commands inside Tcl # prints the present working directory exec pwd # prints the current datetime exec date # creates a new directory in the present working directory exec mkdir new_folder # save the pwd in a variable set p [pwd] # change directory cd another_folder

### Otherscode

 1 2 3 4 # execute a file within a file # so that procs/variables used in another file # are available in the current file source filename

In case if you found something useful to add to this article or you found a bug in the code or would like to improve some points mentioned, feel free to write it down in the comments. Hope you found something useful here.