Created
May 23, 2016 12:54
-
-
Save vkosuri/8b0e7c427a14ce3b545c301836eb06bd to your computer and use it in GitHub Desktop.
TCL commands usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set, a built-in command, reads and writes variables. | |
set a 10 | |
# Desc: puts - Write to a channel | |
# Syntax: puts ?-nonewline? ?channelId? string | |
# channelId stdin, stdout, stderr | |
set abc [puts "test"] | |
puts stdout $tcl_version nonewline | |
# Desc: eval, a built-in Tcl command, interprets its arguments as a script, which it then evaluates. | |
# Syntax: eval arg ?arg ...? | |
set b {the total is $20} | |
# each of the following commands returns a is different result: | |
set a $b | |
eval {set a $b} | |
eval "set a $b" | |
eval [list set a $b] | |
# expr, a built-in Tcl command, evaluates an expression. | |
set a 10 | |
set b 20 | |
set c 100 | |
set res [expr a+b*c] # uses BOMDMAS rule | |
# Desc: global, a built-in Tcl command, declares global variables. | |
# Syntax: global varname ?varname ...? | |
set a 10 | |
proc globalTest {} { | |
global a | |
puts $a | |
} | |
# Description info, a built-in command, provides information about the state of a Tcl interpreter. | |
# info option ?arg arg ...? | |
# info args procname | |
# info body procname | |
# info class subcommand class … | |
# info cmdcount | |
# info commands ?pattern? | |
# info complete script | |
# info coroutine | |
# info default procname arg varname | |
# info errorstack | |
# info exists varName | |
# info frame ?number? | |
# info functions pattern | |
# info globals ?pattern? | |
# info hostname | |
# info level ?number? | |
# info library | |
# info loaded ?interp? | |
# info locals ?pattern? | |
# info nameofexecutable | |
# info object subcommand object … | |
# info patchlevel | |
# info procs ?pattern? | |
# info script ?filename? | |
# info sharedlibextension | |
# info tclversion | |
# info vars ?pattern? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Desc: if, a built-in Tcl command, conditionally evaluates scripts. | |
# Syntax: if expr1 ?then? body1 elseif expr2 ?then? body2 elseif ... ?else? ?bodyN? | |
set a 1 | |
set b stuff | |
if { $a == 1 || | |
$b eq {nonsense}} { | |
puts found | |
} | |
# Desc: while - Execute script repeatedly as long as a condition is met | |
# Syntax: while test body | |
set x 0 | |
while {$x<10} { | |
puts "x is $x" | |
incr x | |
} | |
# Desc: for, a built-in Tcl command, provides an iterative loop construct. | |
# for start test next body | |
for {set x 0} {$x<10} {incr x} { | |
puts "x is $x" | |
} | |
# Desc: [break], a built-in Tcl command, aborts a looping command. | |
# Syntax: break | |
for {set x 0} {$x<10} {incr x} { | |
if {$x > 5} { | |
break | |
} | |
puts "x is $x" | |
} | |
# Desc: continue, a built-in tcl command, skips to the next iteration of a loop | |
# Syntax: continue | |
for {set x 0} {$x<10} {incr x} { | |
if {$x == 5} { | |
continue | |
} | |
puts "x is $x" | |
} | |
# Desc: foreach, a built-in Tcl command, iterates over all elements in one or more lists and evaluates a script at each iteration | |
# Syntax: foreach varlist list body | |
# foreach varlist1 list1 ?varlist2 list2 ... varlistn listn? body | |
foreach a [list 1 2 3 4] b [list 5 6 7 8] c [list a b c d] d [list w x y z] { | |
puts "$a $b $c $d" | |
} | |
# Desc: return, a built-in Tcl command, terminates a script, specifying its result. | |
# Syntax: | |
# return ?result | |
# return ?-code code? ?result | |
# return ?option value? ?result | |
# Options | |
# -errorcode list, -errorinfo info, -errorstack list (Added in TCL 8.6), -leval level, -options options | |
proc foo {} { | |
puts Foo | |
return | |
This is not Tcl - code after the return is never evaluated so | |
may be used for commenting... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# List operations | |
set myList [list 1 2 3 4] | |
# Desc: lindex, a built-in Tcl command, retrieves an element from a list or a nested list. | |
# Syntax: | |
lindex $myList 0 | |
# Desc: llength returns the number of elements in a list. | |
# Syntax: llength list | |
llenght $myList | |
# Desc:lreplace replaces or deletes elements in a list, and can also prepend elements to a list. | |
# Syntax: lreplace list first last ?element element ...? | |
lreplace {a b c} 1 1 hello there | |
# Desc: lappend appends elements to a list. | |
# Syntax: lappend varName ?value value ...? | |
lappend auto_path /usr/local/lib/tcl8.5 | |
# Desc: linsert inserts elements into a list. | |
# Syntax: linsert list index element1 ?element2 element3 ...? | |
set oldList [list 1 2 3 4] | |
set myvalue 10 | |
set newList [linsert $oldList 0 myvalue] | |
# Desc: The lrange command returns one or more adjacent elements from a list. | |
# Syntax: lrange list first last | |
set myList [list a b c d] | |
lrange $myList 0 0 # returns a | |
# Desc: lsearch searches for elements in a list that match a pattern. | |
# Syntax: lsearch ?option? list pattern | |
# Options are | |
# MATCHING STYLE OPTIONS | |
# -exact, -glob, -regexp, -sorted | |
# GENERAL MODIFIER OPTIONS | |
# -all, -inline, -not, -start index | |
# CONTENTS DESCRIPTION OPTIONS | |
# -ascii, -dictionary, -integer, -nocase, -real | |
# SORTED LIST OPTIONS | |
# -decreasing, -increasing, -bisect, -index indexList, -subindices | |
set distros {RedHat SUSE Debian Knoppix Peanut Mandrake Slackware} | |
lsearch $distros RedHat | |
# Desc: lsort, a built-in Tcl command, sorts elements in a list. | |
# Syntax: lsort ?options...? list | |
# Options are | |
# -ascii, -command command, -decreasing, -dictionary, -dictionary mode, -increasing, -index index, -integer, -real | |
lsort -dic {-5 -10 -1 0 3 1 2} # -1 -5 -10 0 1 2 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment