Tcl/TK Tutorial. 2 Learning Tcl/TK What is Tcl/TK? –An interpreted programming language Build on-the-fly commands, procedures Platform-independent Easy.

Slides:



Advertisements
Similar presentations
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Advertisements

Tcl and Otcl Tutorial Part I Internet Computing KUT Youn-Hee Han.
Programming Using Tcl/Tk These slides are based upon u several Tcl/Tk text books u material byDr. Ernest J. Friedman-Hill.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Writing Tcl Scripts (cont.) Outline –Variable Scoping –Strings –Eval –File/Channel I/O –Processes –System Info –Errors –Reflection/Debugging –Libraries.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Tcl/TK Tutorial Fan Yang Kristy Hollingshead
Writing Tcl Scripts Outline Goal Reading Syntax Data Types
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Introduction to JavaScript for Python Programmers
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
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.
Innovation Intelligence ® 1 Chapter 1: Introduction to TCL.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Tcl/Tk: An introduction D. Kim, K. Kundu, and M. Siegel November 26, 2002 CMSC 631.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
Tcl and Otcl Tutorial Part II Internet Computing KUT Youn-Hee Han.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Introduction to Perl Yupu Liang cbio at MSKCC
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Basic Data Types Numbers (integer and floating point)‏ Strings (sequences of characters)‏ Boolean values (true/false)‏
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Programming 101 EPGY Middle School Programming Adam Leeper.
Shell Scripting Lab 2 Advanced Operating System Spring Advanced Operating System - Spring Lab 2.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Programming Using Tcl/Tk Week 2 Seree Chinodom
TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
Introduction to Tcl/Tk TraNese Christy U.S. Army Research Laboratory.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
Dongsoo S. Kim  Tcl: Tool Command Language Interpreted programming (scripting) language Build on-the-fly commands and procedures Embeddable.
Introduction to Programming with Tcl. Introduction Using two languages  C++ for “ data ”  Otcl for control Tcl has been widely used as a scripting language.
Scripting.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
An Introduction To Tcl Scripting John Ousterhout Sun Microsystems Laboratories Tcl/Tk Tutorial, Part II.
TCL/TK Tool Command Language/Tool Kit. What is TCL? u Tool Command Language u An interpreted programming language  Created by John Ousterhout.John Ousterhout.
Perl & TCL Vijay Subramanian, Modified from : perl_basics_06.ppt.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
TCL Training Joshi Sravan Kumar K 21-Nov-2011.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
CSC 352– Unix Programming, Fall 2012
Programming Using Tcl Maxwell Created By: Quan Nguyen Created Date: 12/12/2016 TCL Training1.
TCL/TK Tool Command Language/Tool Kit.
More Selections BIS1523 – Lecture 9.
NS-2 LAB: TCL Miriam Allalouf Semester A.
An Introduction to Python
1. Open Visual Studio 2008.
slides created by Marty Stepp
T. Jumana Abu Shmais – AOU - Riyadh
C Programming Getting started Variables Basic C operators Conditionals
CSC 352– Unix Programming, Fall, 2011
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

Tcl/TK Tutorial

2 Learning Tcl/TK What is Tcl/TK? –An interpreted programming language Build on-the-fly commands, procedures Platform-independent Easy to use for building GUIs Need little experience with programming –Easy –Programs are short, efficient Be willing to learn something new

3 Hello World How to run your Tcl program –Command line (state.cse.ogi.edu or DOS) Type "tclsh" to launch the console –Type your program directly on the console –Use the command "source" (source filename) –Double click your.tcl file (if associated) Output on the console –Command: puts "Hello, world!"

4 Hello World Command line (state.cse.ogi.edu or DOS) –Type "tclsh" to launch the console –Type tcl code into console

5 Hello World Sourced on the console –Type "tclsh", followed by name of program file ######## hello.tcl ####### puts "Hello, world!"

set a 10.0 expr $a + 5 expr int($a/3) set y [expr $a + 9] puts $y

7 Double-clicking your.tcl file (if associated with wish84.exe) ####### hello.tcl ######## Hello.tcl wm withdraw. console show puts "Hello, world!" Hello World

8 Basic operations print to screen ( puts ) puts –nonewline "Hello, world!" puts "!!" assignment ( set ) set income puts "income is $income" (using '$' to get the value of a variable) mathematical expressions ( expr ) set a 10.0 expr $a + 5 expr int($a/3)

9 Some useful commands unset : destroy a variable unset num info : check whether the named variable has been defined if {![info exists num]} { set num 0 } incr num window commands wm withdraw. console show

10 Special characters # : single-line comments, similar to "//" in C ;# : in-line comments, just like "//" in C \ : escape character, same function as in C : also used to break a long line of code to two lines $ : get the value of a variable –var : name of variable –$var : value of variable [] : evaluate command inside brackets

11 Control structures (1) if then else set income if {$income > 30000} { puts "$income -- high" } elseif {$income > 20000} { puts "$income -- middle" } else { puts "$income -- low" } while loops set i 0 while {$i < 100} { puts "I am at count $i" incr i }

set i 100 while {$i > 10} { puts "I am at count $i" set i [expr $i – 1] }

13 Control structures (2) for loops for {set i 0} {$i < 100} {incr i} { puts "I am at count $i and going up" after 300 update } for {set i 100} {$i > 0} {set i [expr $i - 1]} { puts "I am at count $i and going down" } foreach loops set lstColors {red orange yellow green blue purple} foreach c $lstColors { puts $c }

14 Control structures (3) foreach loops (con't) set lstColors {red orange yellow green blue purple} foreach {a b c} $lstColors { puts "$c--$b--$a" } set lstFoods {apple orange banana lime berry grape} foreach f $lstFoods c $lstColors { puts "a $f is usually $c" } foreach {a b} $lstFoods c $lstColors { puts "$a & $b are foods. $c is a color." }

PATH=$PATH:/ns-allinone-2.34 PATH=$PATH:/ns-allinone-2.34/bin export PATH

16 Procedures procedure calls (embedded commands) set b [expr $a + 5] puts "The value of b is $b" create your own procedure (called by value only) proc foo {a b c} { return [expr $a * $b - $c] } puts [expr [foo 2 3 4] + 5] proc bar { } { puts "I'm in the bar procedure" } bar

proc oe {a} { set r [expr $a % 2] if { $r == 0} { puts “Even” } else { puts “Odd” } oe 11

18 Variable scope set a 5 set b 6 set c 7 proc var_scope { } { global a set a 3 set b 2 set c 1 } var_scope puts "The value for a b c is: $a $b $c"

19 Lists in Tcl/TK Everything is a list! Many ways to create a list set myList [list a b c] set myList "a b c" set myList {a b c} set myList [list $a $b $c] set myList {$a $b $c} set myList [list a b c] set myList "a b c" set s Hello puts "The length of $s is [string length $s]." => The length of Hello is 5. puts {The length of $s is [string length $s].} => The length of $s is [string length $s].

20 List operations set lstStudents [list "Fan" "Kristy" "Susan"] puts [lindex $lstStudents 0] puts [lindex $lstStudents end] puts [llength lstStudents] (unexpected result!) puts [llength $lstStudents] lappend $lstStudents "Peter" (wrong!) lappend lstStudents "Peter" puts [linsert lstStudents 2 "Tom"] (wrong!) puts [linsert $lstStudents 2 "Tom"] set lstStudents [linsert $lstStudents 2 "Tom"] set lstStudents [lreplace $lstStudents 3 3 "Rachel"] set lstStudents [lreplace $lstStudents end end] set lstStudents [lsort –ascii $lstStudents] puts [lsearch $lstStudents "Peter"]

21 Lists of lists (of lists…) set a [list [list x y z]] puts [lindex $a 0] puts [lindex [lindex $a 0] 1] puts [lindex [lindex $a 1] 0] (unexpected result) set a [list x [list [list y] [list z]]] => How to get to the z? set arg1 [list g [list f [list h [list i X]]] [list r Y] k] set arg2 [list g [list f [list h [list i Y]]] [list r b] L] set both [list $arg1 $arg2] puts $both

22 Array operations Associative arrays (string as index) set color(rose) red set color(sky) blue set color(medal) gold set color(leaves) green set color(blackboard) black puts [array exists color] (tests if an array with the name "color" exists) puts [array exists colour] puts [array names color] (returns a list of the index strings) foreach item [array names color] { puts "$item is $color($item)" } (iterating through array) set lstColor [array get color] (convert array to list) array set color $lstColor (convert list to array)

23 Regular expressions regsub set stmt "Fan is one of Shania’s fans" regsub –nocase "fan" $stmt "Kristy" newStmt ?switches? exp string subSpec ?varName? puts "$newStmt" regsub –nocase –all "fan" $stmt "Kristy" newStmt puts "$newStmt" regexp (returns 1 if the regular expression matches the string, else returns 0) puts [regexp –nocase "fan" $stmt] ?switches? regexp string format puts [format "%s is a %d-year-old" Fan 26] formatString ?arg arg...?

24 String operations set statement " Fan is a student " set statement [string trim $statement] puts [string length $statement] puts [string length statement] puts [string index $statement 4] puts [string index $statement end] puts [string first "is" $statement] (string last) puts [string first $statement "is"] puts [string range $statement 4 end] puts [string replace $statement 9 end "professor"] puts [string match "*student" $statement] (* ? [])

25 File operations set fRead [open source.txt r] set fWrite [open target.txt w] while {![eof $fRead]} { set strLine [gets $fRead] ;#or gets $fRead strLine regsub –nocase –all "fan" $strLine "kristy" strLine puts $fWrite $strLine } close $fRead close $fWrite ################ source.txt ################ Fan is a CSE student. Fan is also one of Shania’s fans. Kristy and Fan are classmates.

26 Miscellaneous commands eval: execute a command dynamically built up in your program set Script { set Number1 17 set Number2 25 set Result [expr $Number1 + $Number2] } eval $Script exec: execute external programs clock

27 Debugging your program Use puts statements ( with update and after when using wish84.exe to run program) tk_messageBox: pop up a message box tk_messageBox –message "run to here" –type ok tclpro trace variable variableName operation procedure

28 Common pitfalls Missing $ or extraneous $ Using {a} vs "a" vs [list a] Creating list items that are empty lists a b {} d

29 Maze Tcl example pseudocode: create a path which just has the start state make this path the only member of the list of alternatives to be explored while list of alternatives is not empty and not done set firstpath to be the first path from the list of alternatives update alternatives so it doesn't include the first path set last to be the last member of firstpath for each cell connected to the last member create newpath with cell at the end of firstpath if cell is 16 display path else add newpath to end of list of alternatives

30 Maze Tcl example set bDone 0 set path [list 1] set alternatives [list $path] while {[llength $alternatives] > 0 && !$bDone} { set firstpath [lindex $alternatives 0] set alternatives [lrange $alternatives 1 end] set last [lindex $firstpath end] foreach cell $connected($last) { set newpath [linsert $firstpath end $cell] if {$cell == 16} { puts "Answer is $newpath" set bDone 1 break update after 1000 } else { lappend alternatives $newpath }