Innovation Intelligence ® 1 Chapter 1: Introduction to TCL.

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

The Web Warrior Guide to Web Design Technologies
Lecture 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Tcl and Otcl Tutorial Part I Internet Computing KUT Youn-Hee Han.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Writing Tcl Scripts Outline Goal Reading Syntax Data Types
Tcl/Tk 2 CS 414, Software Engineering I Mark Ardis Rose-Hulman Institute December 5, 2002.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Guide To UNIX Using Linux Third Edition
JavaScript, Third Edition
Introduction to C Programming
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Nael Alian Introduction to PHP
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
2440: 211 Interactive Web Programming Expressions & Operators.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Linux+ Guide to Linux Certification, Third Edition
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Tcl/TK Tutorial. 2 Learning Tcl/TK What is Tcl/TK? –An interpreted programming language Build on-the-fly commands, procedures Platform-independent Easy.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
JavaScript, Fourth Edition
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Tcl/Tk TraNese Christy U.S. Army Research Laboratory.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Innovation Intelligence ® 1 Chapter 4: Using TCL to Control the HyperMesh Session.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introduction to Programming with Tcl. Introduction Using two languages  C++ for “ data ”  Otcl for control Tcl has been widely used as a scripting language.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Linux Administration Working with the BASH Shell.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
An Introduction To Tcl Scripting John Ousterhout Sun Microsystems Laboratories Tcl/Tk Tutorial, Part II.
第二章 TCL 語言簡介 報告者:童耀民. 變數 (variable) 和變數替換 (variable substituion) set foo "john" puts "my name is $foo“ set month 2 set day 3 set year 97 set.
Chapter 6 JavaScript: Introduction to Scripting
2.5 Another Java Application: Adding Integers
Introduction to Scripting
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
TCL/TK Tool Command Language/Tool Kit.
An Introduction to Python
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 3 – Introduction to C# Programming
Selection Statements Chapter 3.
Presentation 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 quotes This 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 Albany set Z_LABEL “The Capitol of New York is: “ puts “$Z_LABEL $Z” ; # Prints the value of Z puts “$Z_LABEL \$Z”; #Prints a literal $Z instead of the value of Z Output Albany The Capitol of New York is: The Capitol of New York is: Albany The Capitol of New York is: $Z

Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 10 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. 11 TCL Command Overview – Variables Multiple variable substitution Input: set month 2 set day 3 set year 09 set 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. 12 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. 13 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. 14 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 Output: 9

Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 15 Mathematical functions supported in expressions: Input: expr sin(2) Output: TCL Command Overview – Expressions abscoshlogsqrt acosdoublelog10srand asinexppowtan atanfloorrandtanh atan2fmodroundwide ceilhypotsin cosintsinh

Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 16 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 feet tall

Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 17 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. 18 TCL Command Overview – Comparisons & Loops Using if-else statements if test1 body1 ?elseif test2 body2 elseif...? ?else bodyn? 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." } 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. 19 TCL Command Overview – Comparisons & Loops Using switch statements switch ?options? string {pattern body ?pattern body...?} Input: set num_legs 4 switch $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. 20 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 == 0 In the for loop, and i == 1 In the for loop, and i == 2 In the for loop, and i == 3 In the for loop, and i == 4

Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 21 TCL Command Overview – Comparisons & Loops Using while statements while test body Input: set i 0 while {$i < 5} { puts "In the while loop, and i == $i" incr i 1 } Output: In the while loop, and i == 0 In the while loop, and i == 1 In the while loop, and i == 2 In the while loop, and i == 3 In the while loop, and i == 4

Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 22 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 vowel e is a vowel i is a vowel o is a vowel u is a vowel

Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 23 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. 24 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: John Mary 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. 25 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. 26 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. 27 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. 28 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 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 The index of the first occurrence of letter “i” is: 2

Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 29 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. 30 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 12 set num2 14 set 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 26 The magnitude of 3 is 3 The magnitude of -2 is 2

Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 31 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. 32 TCL Command Overview – Namespace The namespace eval command lets you create new namespaces. For example, namespace eval Counter { namespace export bump variable num 0 proc bump {} { variable num incr 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. 33 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. 34 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. 35 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. 36 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. 37 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 $filename Output: C:/Documents and Settings/training/My Documents/test.txt

Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 38 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. 39 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: