Introduction to Programming with Tcl. Introduction Using two languages  C++ for “ data ”  Otcl for control Tcl has been widely used as a scripting language.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

M AT L AB Programming: scripts & functions. Scripts It is possible to achieve a lot simply by executing one command at a time on the command line (even.
LAB 3 NS2 Preliminaries. Contents TCL/OTCl Fundamentals Creating Network Setting Connections Generating Traffic Inserting Errors Configuring for multicast.
Adapted from John Zelle’s Book Slides
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.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
NS-2 LAB: TCL Semester A Miriam Allalouf.
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
Writing Tcl Scripts (cont.) Outline –Variable Scoping –Strings –Eval –File/Channel I/O –Processes –System Info –Errors –Reflection/Debugging –Libraries.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
Tcl/TK Tutorial Fan Yang Kristy Hollingshead
Writing Tcl Scripts Outline Goal Reading Syntax Data Types
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
PHP : Hypertext Preprocessor
PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Innovation Intelligence ® 1 Chapter 1: Introduction to TCL.
WML II (“Son of WML”) WML WMLScript. WML - A Quick Review Document structure ,... Text and image controls ...,,..., Navigation controls ,,, Events.
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Chapter 6: Shell Programming
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
Tcl/Tk: An introduction D. Kim, K. Kundu, and M. Siegel November 26, 2002 CMSC 631.
Tcl and Otcl Tutorial Part II Internet Computing KUT Youn-Hee Han.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
1 Shell Scripting (C shell) SungHo Maeung 10/27/2000 Tutorial section Computer Science Lab.
Introduction to Awk Awk is a convenient and expressive programming language that can be applied to a wide variety of computing and data manipulation tasks.
Tcl/TK Tutorial. 2 Learning Tcl/TK What is Tcl/TK? –An interpreted programming language Build on-the-fly commands, procedures Platform-independent Easy.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 05.
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
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming (2) Ruibin Bai (Room AB326) Division of Computer Science The University.
PHP - 1h. How it works Client requests document Server loads document in memory Server processes document with relevant module (PHP) Server sends XHTML.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
LIN Unix Lecture 7 Hana Filip. LIN Text Processing Command Line Utility Programs (cont.) sed LAST WEEK wc sort tr uniq awk TODAY join paste.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Shell Scripting Lab 2 Advanced Operating System Spring Advanced Operating System - Spring Lab 2.
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 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.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
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.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Dongsoo S. Kim  Tcl: Tool Command Language Interpreted programming (scripting) language Build on-the-fly commands and procedures Embeddable.
Scripting.
The Scripting Programming Language
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
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.
第二章 TCL 語言簡介 報告者:童耀民. 變數 (variable) 和變數替換 (variable substituion) set foo "john" puts "my name is $foo“ set month 2 set day 3 set year 97 set.
TCL Training Joshi Sravan Kumar K 21-Nov-2011.
Python: Experiencing IDLE, writing simple programs
Control Flow Constructs: Conditional Logic
Programming Using Tcl Maxwell Created By: Quan Nguyen Created Date: 12/12/2016 TCL Training1.
TCL/TK Tool Command Language/Tool Kit.
NS-2 LAB: TCL Miriam Allalouf Semester A.
Arrays Topics Definition of a Data Structure Definition of an Array
Chopin’s Nocturne.
Arrays Topics Definition of a Data Structure Definition of an Array
Presentation transcript:

Introduction to Programming with Tcl

Introduction Using two languages  C++ for “ data ”  Otcl for control Tcl has been widely used as a scripting language. Tcl is used in combination with the Tk ("Tool Kit") library

Tcl Programming Basics Variables and Variable Substitution Expressions Command Substitution Control Flow Procedures Lists Arrays Strings Input/Output Other Miscellaneous Tcl Commands

Variables and Variable Substitution Example 1.1: set foo "john" puts "Hi my name is $foo" Output: Hi my name is john

Variables and Variable Substitution Example 1.2: set month 2 set day 3 set year 97 set date "$month:$day:$year" puts $date Output: 2:3:97

Variables and Variable Substitution Example 1.3: set foo "puts hi" eval $foo Output: hi

Expressions Example 2.1: set value [expr 0==1] put $value Output : 0 Example 2.2: set value [expr 2>=1] put $value Output : 1

Expressions Example 2.3: set value [expr 2+1] put $value Output: 5

Command Substitution Example 3.1: puts "I am [expr 10 * 2] years old, and my I.Q. is [expr ]" Output: I am 20 years old, and my I.Q. is 75

Command Substitution Example 3.2: 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

Control Flow Example 4.1: set my_planet "earth" if { $my_planet == "earth“ } { puts "I feel right at home." } elseif { $my_planet == "venus“ } { puts "This is not my home." } else { puts "I am neither from Earth, nor from Venus." }

Control Flow set temp 95 if { $temp < 80 } { puts "It's a little chilly." } else { puts "Warm enough for me." } Output: I feel right at home. Warm enough for me.

Control Flow Example 4.2: 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.

Control Flow Example 4.3: 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

Control Flow Example 4.4: set i 0 while {$i < 10} { 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

Control Flow Example 4.5: 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

Procedures Example 5.1: proc sum_proc {a b} { return [expr $a + $b] } proc magnitude {num} { if {$num > 0} { return $num } set num [expr $num * (-1)] return $num }

Procedures 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

Procedures Example 5.2: proc dumb_proc {} { set myvar 4 puts "The value of the local variable is $myvar" global myglobalvar puts "The value of the global variable is $myglobalvar" } set myglobalvar 79 dumb_proc Output: The value of the local variable is 4 The value of the global variable is 79

Lists Example 6.1: set simple_list "John Joe Mary Susan" puts [lindex $simple_list 0] puts [lindex $simple_list 2] Output: John Mary

Lists Example 6.2: set simple_list2 "Mike Sam Heather Jennifer" set compound_list [list $simple_list $simple_list2] puts $compound_list puts [llength $compound_list] Output: {John Joe Mary Susan} {Mike Sam Heather Jennifer} 2

Lists Example 6.3: set mylist "Mercury Venus Mars" puts $mylist set mylist [linsert $mylist 2 Earth] puts $mylist lappend mylist Jupiter puts $mylist Output: Mercury Venus Mars Mercury Venus Earth Mars Mercury Venus Earth Mars Jupiter

Arrays Example 7.1: set myarray(0) "Zero" set myarray(1) "One" set myarray(2) "Two" for {set i 0} {$i < 3} {incr i 1} { puts $myarray($i) } Output: Zero One Two

Arrays Example 7.2: set person_info(name) "Fred Smith" set person_info(age) "25" set person_info(occupation) "Plumber" foreach thing {name age occupation} { puts "$thing == $person_info($thing)" } Output: name == Fred Smith age == 25 occupation == Plumber

Arrays Example 7.3: set person_info(name) "Fred Smith" set person_info(age) "25" set person_info(occupation) "Plumber" foreach thing [array names person_info] { puts "$thing == $person_info($thing)" } Output: occupation == Plumber age == 25 name == Fred Smith

Strings Example 8.1: set str "This is a string" 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 4 through 8 are: [string range $str 4 8]" puts "The index of the first occurrence of letter \"i\" is: [string first i $str]"

Strings Output: The string is: This is a string The length of the string is: 16 The character at index 3 is: s The characters from index 4 through 8 are: is a The index of the first occurrence of letter "i" is: 2

Input/Output Example 9.1: puts -nonewline "Enter your name: " set bytesread [gets stdin name] puts "Your name is $name, and it is $bytesread bytes long" Output: (note that user input is shown in italics) Enter your name: Shyam Your name is Shyam, and it is 5 bytes long

Input/Output Example 9.2: set f [open "/tmp/myfile" "w"] puts $f "We live in Texas. It's already 110 degrees out here." puts $f "456" close $f Output: (none)

Input/Output Example 9.3: set f [open "/tmp/myfile" "r"] set line1 [gets $f] set len_line2 [gets $f line2] close $f puts "line 1: $line1" puts "line 2: $line2" puts "Length of line 2: $len_line2" Output: line 1: We live in Texas. It's already 110 degrees out here. line 2: 456 Length of line 2: 3

Other Miscellaneous Tcl Commands Example 10.1: set foo "set a 22" eval $foo puts $a Output: 22

Other Miscellaneous Tcl Commands Example 10.2: set retval [catch {set f [open "nosuchfile" "r"]}] if {$retval == 1} { puts "An error occured" } Output: (this output occurs if there is no file named "nosuchfile" in the current directory). An error occured