innovation intelligence ® 1 chapter 1: introduction to tcl
TRANSCRIPT
Innovation Intelligence®
1
Chapter 1: Introduction to TCL
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
22
TCL/Tk Introduction
• About TCL
• About Tk
• Basic TCL Syntax
• TCL Command Overview
• Tk Basic Commands
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
33
TCL/Tk Introduction – About TCL and Tk
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
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
44
TCL/Tk Introduction – Basic Tcl Syntax
• First item of a command line is a command name.
• The words in the command line are separated by one or more spaces.
• Words can be grouped with double quotes or curly braces.
• Commands are terminated with new line or semi-colon.
• A word starting with a dollar sign ($) must be a variable name. The string will be replaced by the value of the variable.
• Words enclosed within square brackets must be a legal Tcl command. The strings would be replaced by the results of evaluating the command.
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
55
TCL/Tk Introduction – TCL Command Overview
• Text Output
• Variables and Variable Substitution
• Expressions
• Command Substitution
• Comparisons and Loops
• Lists
• Arrays
• Strings
• Procedures
• Namespaces
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
66
TCL Command Overview – Text Output
• Print a string using puts command
Input: puts HyperWorks
Output: HyperWorks
• If string is more than one word, the string must be enclosed in either double quotes (“ “) or braces ({ })
Input: puts “This is an example with quotes”puts {This is an example with braces}
Output: This is an example with quotesThis is an example with braces
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
77
TCL Command Overview – Text Output
• TCL command terminated with newline or semicolon ;
• Comments are designated with # at beginning of line or after semicolon
Input: # An example using a semicolon
puts “This is line 1”; puts {This is line 2}; #Note after the ;
Output: This is line 1
This is line 2
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
88
TCL Command Overview – Variables
• Variables do not need to be declared before using
• Create variables using set command
• Delete variables using unset command
• Variable substitution done using $ to access value stored in variable
Input: set software "HyperWorks"puts "The software we are using is $software"
Output: The software we are using is HyperWorks
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
99
TCL Command Overview – Variables
• Variable substitution inside strings• Difference between using double quotes and braces
• Variable substitution with double quotes• Using \ before variable results in literal value being printed
Input: set Z Albanyset Z_LABEL “The Capitol of New York is: “puts “$Z_LABEL $Z” ; # Prints the value of Zputs “$Z_LABEL \$Z”; #Prints a literal $Z instead of the
value of Z
Output AlbanyThe Capitol of New York is: The Capitol of New York is: AlbanyThe Capitol of New York is: $Z
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
1010
TCL Command Overview – Variables
• Variable substitution with braces• Braces disable the substitution of variables with braces
Input: puts "$Z_LABEL $Z"puts {$Z_LABEL $Z}
Output: The Capitol of New York is: Albany$Z_LABEL $Z
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
1111
TCL Command Overview – Variables
• Multiple variable substitution
Input: set month 2set day 3set year 09set date "$month:$day:$year"puts $date
Output: 2:3:09
• Using eval command
Input: set foo “puts hi”eval $foo
Output: hi
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
1212
TCL Command Overview – Expressions
• Expressions are evaluated using expr command
• Mathematical and rational expressions allowed
• Expressions consist of operands, operators, parenthesis• White space between these are allowed
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
1313
TCL Command Overview – Expressions
• Operands can be:• Numerical values (integer or floating-point)• TCL variable using $• String enclosed in double quotes or braces• TCL command enclosed in brackets
• Operators can be:• - + ~ ! Unary minus, unary plus, bit-wise NOT, logical NOT. • * / % Multiply, divide, remainder. • + - Add and subtract. • << >> Left and right shift. • < > <= >= Boolean less, greater, less than or equal, and greater than or equal.• == != Boolean equal and not equal. • & Bit-wise AND. • ^ Bit-wise exclusive OR. • | Bit-wise OR. • && Logical AND. • || Logical OR.
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
1414
TCL Command Overview – Expressions
• Rational Expression• 0 is false; 1 is true
Input: expr 0 == 1
Output: 0
Input: expr 1 == 1
Output: 1
• Mathematical Expression
Input: expr 4 + 5
Output: 9
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
1515
• Mathematical functions supported in expressions:
Input: expr sin(2)
Output: 0.909297
TCL Command Overview – Expressions
abs cosh log sqrt
acos double log10 srand
asin exp pow tan
atan floor rand tanh
atan2 fmod round wide
ceil hypot sin
cos int sinh
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
1616
TCL Command Overview – Command Substitution
• Square brackets [ ] are used to achieve command substitution
• Text between [ ] are evaluated and its result is substituted in its place
Input: set my_height 6.0
puts "If I was 2 inches taller, I would be [expr $my_height + (2.0 / 12.0)] feet tall"
Output: If I was 2 inches taller, I would be 6.16667 feet tall
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
1717
TCL Command Overview – Comparisons & Loops
• Decision making commands• If-else statements• Switch statement
• Looping statements• while• for• foreach
• Commands can alter the flow of execution in response to some condition
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
1818
TCL Command Overview – Comparisons & Loops
• Using if-else statements• if test1 body1 ?elseif test2 body2 elseif ...? ?else bodyn?
Input: set k 35if {$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."
}
Output: Handling is good.
• Indentations are not required, but they make it easier to read the code
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
1919
TCL Command Overview – Comparisons & Loops
• Using switch statements• switch ?options? string {pattern body ?pattern body ...?}
Input: set num_legs 4switch $num_legs {
2 {puts "It could be a human."}4 {puts "It could be a cow."}6 {puts "It could be an ant."}8 {puts "It could be a spider."}default {puts "It could be anything."}
}
Output: It could be a cow.
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
2020
TCL Command Overview – Comparisons & Loops
• Using for statements• for init test reinit body
Input: for {set i 0} {$i < 5} {incr i 1} {puts "In the for loop, and i == $i"
}
Output: In the for loop, and i == 0In the for loop, and i == 1In the for loop, and i == 2In the for loop, and i == 3In the for loop, and i == 4
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
2121
TCL Command Overview – Comparisons & Loops
• Using while statements• while test body
Input: set i 0while {$i < 5} {
puts "In the while loop, and i == $i"incr i 1
}
Output: In the while loop, and i == 0In the while loop, and i == 1In the while loop, and i == 2In the while loop, and i == 3In the while loop, and i == 4
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
2222
TCL Command Overview – Comparisons & Loops
• Using foreach statements• foreach varName list body
Input: foreach vowel {a e i o u} {puts "$vowel is a vowel"
}
Output: a is a vowele is a voweli is a vowelo is a vowelu is a vowel
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
2323
TCL Command Overview – Lists
• Simple way to group items
• Deal with the collection of items as a single entity
• Lists can be defined by:
• A string:
• set lst “item1 item2 item3”
• A variable to be a list of values
• set lst {{item1} {item2} {item3}}
• Using the split command
• set lst [split “item1.item2.item3” “.”]
• Using the list command
• set lst [list “item1” “item2” “item3”]
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
2424
TCL Command Overview – Lists
• An individual member of list can be accessed with lindex
Input: set simple_list “John Joe Mary Susan”puts [lindex $simple_list 0]puts [lindex $simple_list 2]
Output: JohnMary
• List indexing is zero-based
• Lists can be iterated through using the foreach command
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
2525
TCL Command Overview – Lists
• The length of a list can be determined with llength
Input: set names {Canada India USA UK} puts “List length is: [llength $names]”
Output: List length is: 4
• A value can be inserted into a list using linsert
Input: set names {Canada India USA UK}set newnames [linsert $names 2 China]puts “New list is: $newnames”
Output: New list is: Canada India China USA UK
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
2626
TCL Command Overview – Lists
• A list can be appended to using the lappend command
Input: set names {Canada India USA UK} set newnames [lappend names China]puts “New list is: $newnames”
Output: New list is: Canada India USA UK China
• Notice how with lappend the name of a variable containing the list (names) is used rather than the list itself ($names).
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
2727
TCL Command Overview – Arrays
• Unlike arrays in many other languages, Tcl arrays are indexed by keywords. The keywords can be easy to remember strings or integers, just as you like.
• 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
• The array command is used in Tcl to manipulate array data.
Input: array set fruitColors {tomato red banana yellow} puts $fruitColors(tomato)
Output: red
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
2828
TCL Command Overview – Strings
• Strings are a set of alphanumeric characters stored and manipulated together
• 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 CanadaThe length of the string is: 14The character at index 3 is: sThe characters from index 3 through end are: s is CanadaThe index of the first occurrence of letter “i” is: 2
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
2929
TCL Command Overview – Procedures
• TCL procedures similar to functions in C
• Procedures may take arguments and may return values
• The basic syntax for defining a procedure is:
• proc name argList body
• Once a procedure is created, it is considered to be a command
• 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
• However, to return another value, the return command may be used
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
3030
TCL Command Overview – Procedures
Input: proc sum_proc {a b} {return [expr $a + $b]
}proc magnitude {num} {
if {$num > 0} {return $num
}set num [expr $num * (-1)]return $num
}set num1 12set num2 14set sum [sum_proc $num1 $num2]puts "The sum is $sum"puts "The magnitude of 3 is [magnitude 3]"puts "The magnitude of -2 is [magnitude -2]"
Output: The sum is 26The magnitude of 3 is 3The magnitude of -2 is 2
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
3131
TCL Command Overview – Namespace
• A namespace is a collection of commands and variables.
• Create and manipulate contexts for commands and variables
• It encapsulates the commands and variables to ensure that they won't interfere with the commands and variables of other namespaces.
• Tcl has always had one such collection, which we refer to as the global namespace.
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
3232
TCL Command Overview – Namespace
• The namespace eval command lets you create new namespaces. For example,
namespace eval Counter {namespace export bump
variable num 0proc bump {} {
variable numincr num
}}
• Creates a new namespace containing the variable num and the procedure bump.
• The commands and variables in this namespace are separate from other commands and variables in the same program.
• If there is a command named bump in the global namespace, for example, it will be different from the command bump in the Counter namespace.
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
3333
TCL/Tk Introduction – Tk Basic Commands
• tk_getOpenFile
• tk_getSaveFile
• tk_chooseDirectory
• tk_messageBox
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
3434
Tk Basic Commands - tk_getOpenFile
• tk_getOpenFile pops up a dialog box for the user to select a file to open
• Associated with the Open command in the File menu • Its purpose is for the user to select an existing file only • This dialog box does not open a file, it simply returns the filename so that
it can be used in your script.
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
3535
Tk Basic Commands - tk_getOpenFile
Input: set filename [tk_getOpenFile]puts $filename
Output:
C:/Documents and Settings/training/My Documents/autosave.mvw
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
3636
Tk Basic Commands - tk_getOpenFile
• Additional options can be used with the following format:
tk_getOpenFile ?option value ...?
• Example with option –filetypes
set types {{{Text Files} {.txt} }{{TCL Scripts} {.tcl}}{{All Files} * }
}
set filename [tk_getOpenFile -filetypes $types]
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
3737
Tk Basic Commands - tk_getSaveFile
• tk_getSaveFile pops up dialog box for user to select a file to save
• Associated with the Saveas command in the File menu
• This dialog box does not save a file, it simply returns the filename so that it can be used in your script
• Behaves the same way as tk_getOpenFile procedure
• Have the same options
• Example using –title option
Input: set filename [tk_getSaveFile –title “Select a File”] puts $filenameOutput: C:/Documents and Settings/training/My Documents/test.txt
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
3838
Tk Basic Commands - tk_chooseDirectory
• tk_chooseDirectory pops up a dialog box for the user to select a directory
• Allows user to set a directory name to a variable to be used in a script
Input: set dirname [tk_chooseDirectory]
puts $dirname
Output: C:/temp
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.
3939
Tk Basic Commands - tk_messageBox
• tk_messageBox procedure creates and displays a message window with
an application-specified message, an icon, and a set of buttons.
• The following example illustrates the tk_messageBox command with the
message option.
• This option allows you to specify the message to be displayed in the
message box.
• Input: tk_messageBox -message "This is the message to be displayed"
• Output: