Chapter 1: Introduction To TCL: Innovation Intelligence
Chapter 1: Introduction To TCL: Innovation Intelligence
Innovation
Intelligence®
1
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
TCL/Tk Introduction
• About TCL
• About Tk
• Tk Basic Commands
2
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
About TCL
• Simple and programmable syntax
• Can be used as standalone application or embedded in programs
• Open source
• Interpreted language
• New TCL commands can be implemented using C language
About Tk
• Graphical user interface (GUI) toolkit
• Tk adds about 35 TCL commands
• Create and manipulate widgets
• Widget is a window in GUI with particular appearance and behavior
3
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
• The words in the command line are separated by one or more spaces.
• A word starting with a dollar sign ($) must be a variable name. The string
will be replaced by the value of the variable.
4
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
• Expressions
• Command Substitution
• Lists
• Arrays
• Strings
• Procedures
• Namespaces
5
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
Output: HyperWorks
• If string is more than one word, the string must be enclosed in either
double quotes (“ “) or braces ({ })
6
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
7
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
8
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
9
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
10
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
Output: 2:3:09
Output: hi
11
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
12
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
13
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
Input: expr 0 == 1
Output: 0
Input: expr 1 == 1
Output: 1
• Mathematical Expression
Input: expr 4 + 5
Output: 9
14
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
Output: 0.909297
15
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
• Text between [ ] are evaluated and its result is substituted in its place
16
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
• Looping statements
• while
• for
• foreach
17
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
Input: set k 35
if {$k == 35} {
puts "Handling is good."
} elseif {$k == 20} {
puts "Ride is good."
} else {
puts "I am not sure of the quality of ride or
handling."
}
• Indentations are not required, but they make it easier to read the code
18
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
19
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
20
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
21
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
Output: a is a vowel
e is a vowel
i is a vowel
o is a vowel
u is a vowel
22
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
23
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
Output: John
Mary
24
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
25
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
• Notice how with lappend the name of a variable containing the list
(names) is used rather than the list itself ($names).
26
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
• The values of the array elements can also be strings or numbers. A Tcl
array is created when you assign the first array element:
Input: set myArray(foo) "bar"
puts $myArray(foo)
Output: bar
Output: red
27
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
• All data items in Tcl, including numeric values, are treated as strings.
• They are treated as other data types only as needed.
• This makes string manipulation and the associated commands very important and
frequently utilized.
Input:
set str "This is Canada"
puts "The string is: $str"
puts "The length of the string is: [string length $str]"
puts "The character at index 3 is: [string index $str 3]"
puts "The characters from index 3 through end are: [string range $str 3 end]"
puts "The index of the first occurrence of letter \"i\" is: [string first i $str]“
Output:
This is Canada
The string is: This is Canada
The length of the string is: 14
The character at index 3 is: s
The characters from index 3 through end are: s is Canada
28
The index of the first occurrence of letter “i” is: 2
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
• Called using its name, followed by a value for each of its arguments
• By default, the return value from a procedure is the result of the last
command in its body
29
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
30
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
• Tcl has always had one such collection, which we refer to as the global
namespace.
31
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
32
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
• tk_getSaveFile
• tk_chooseDirectory
• tk_messageBox
33
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
34
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
Output:
35
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
set types {
{{Text Files} {.txt} }
{{TCL Scripts} {.tcl}}
{{All Files} * }
}
36
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
• This dialog box does not save a file, it simply returns the filename so that it can be
used in your script
37
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
puts $dirname
Output: C:/temp
38
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
39