Tcl and Otcl Tutorial Part I Internet Computing KUT Youn-Hee Han.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

15. Python - Modules A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Building Java Programs
1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Return values.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Logic & program control part 2: Simple selection structures.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Programming Using Tcl/Tk These slides are based upon u several Tcl/Tk text books u material byDr. Ernest J. Friedman-Hill.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Concepts when Python retrieves a variable’s value Namespaces – Namespaces store information about identifiers and the values to which they are bound –
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
BIL101, Introduction to Computers and Information Systems Chapter 12 A Portable Scientific Visualization Program: GnuPlot Prepared by Metin Demiralp Istanbul.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
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.
C++ for Engineers and Scientists Third Edition
Introduction to C Programming
1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
1 Python Chapter 2 © Samuel Marateck, After you install the compiler, an icon labeled IDLE (Python GUI) will appear on the screen. If you click.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Innovation Intelligence ® 1 Chapter 1: Introduction to TCL.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Week 1 Algorithmization and Programming Languages.
CPS120: Introduction to Computer Science Decision Making in Programs.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Chapter 05 (Part III) Control Statements: Part II.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Computer Science 121 Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
An Introduction To Tcl Scripting John Ousterhout Sun Microsystems Laboratories Tcl/Tk Tutorial, Part II.
Perl & TCL Vijay Subramanian, Modified from : perl_basics_06.ppt.
Week 3 - Monday.  What did we talk about last time?  Using Scanner to get input  Basic math operations  Lab 2.
Lecture 3 Expressions, Type Conversion, Math and String
CSC 4630 Meeting 7 February 7, 2007.
Chapter 6 - Functions modular programming general function format
Introduction to Programming
JavaScript: Control Statements.
TCL/TK Tool Command Language/Tool Kit.
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Chapter 8 JavaScript: Control Statements, Part 2
C Programming Getting started Variables Basic C operators Conditionals
Terminal-Based Programs
Just Enough Java 17-May-19.
Introduction to Bash Programming, part 3
Problem 1 Given n, calculate 2n
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Tcl and Otcl Tutorial Part I Internet Computing KUT Youn-Hee Han

Data Structure2 Prime Number - Batch Mode prime.tcl if {$argc != 1} { puts stderr "ERROR!" exit 1 } else { set j [lindex $argv 0] } proc prime {j} { for {set a 2} {$a <= $j} {incr a} { set b 0 for {set i 2} {$i < $a} {incr i} { set d [expr fmod($a,$i)] if {$d==0} { set b 1 } if {$b == 1} { puts "$a is not a prime number" } else { puts "$a is a prime number" } prime $j # tclsh prime 5

Data Structure3 Output puts command If the string has more than one word, you must enclose the string in double quotes or braces ({}). % puts Good Good % puts Good Morning can not find channel named "Good“ % puts “Good Morning” Good Morning puts "Hello, World - In quotes" ;# This is a comment after the command. # This is a comment at beginning of a line puts {Hello, World - In Braces} puts "This is line 1"; puts "this is line 2" puts "Hello, World; - With a semicolon inside the quotes" # Words don't need to be quoted unless they contain white space: puts HelloWorld

Data Structure4 Variable Typeless variable Variable declaration needed only to global variable set command $ prefix set X "This is a string" set Y 1.24 puts $X puts $Y puts " " set label "The value in Y is: " puts "$label $Y" set name “han” puts $name puts “My name is $name” Name = “Jone” ;# Error

Data Structure5 Evaluation & Substitution In Tcl, the evaluation of a command is done in 2 phases. The first phase is a single pass of substitutions. The second phase is the evaluation of the resulting command. Note that only one pass of substitutions is made. Ex.] Assuming we have set varName to "Hello World", the sequence would look like this: puts $varName ⇒ puts "Hello World", which is then executed and prints out Hello World. A command within square brackets ([]) is replaced with the result of the execution of that command. puts $varName

Data Structure6 Evaluation & Substitution Double quotes (“”) Grouping words double quotes allows substitutions to occur within the quotations Backslash Sequence  Any character immediately following the backslash (/) will stand without substitution.  the backslash at the end of a line of text causes the interpreter to ignore the newline  Other example

Data Structure7 Evaluation & Substitution Double quotes (“”) 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 puts "\nBen Franklin is on the \$ bill" set a puts "Washington is not on the $a bill" ;# This is not what you want puts "Lincoln is not on the $$a bill" ;# This is OK puts "Hamilton is not on the \$a bill" ;# This is not what you want puts "Ben Franklin is on the \$$a bill" ;# But, this is OK puts "\n examples of escape strings" puts "Tab\tTab\tTab" puts "This string prints out \non two lines" puts "This string comes out\ on a single line"

Data Structure8 Evaluation & Substitution The brace grouping ({}) braces is used only when they are used for grouping grouping words within double braces disables substitution within the braces. Characters within braces are passed to a command exactly as written. The only "Backslash Sequence" that is processed within braces is the backslash at the end of a line. This is still a line continuation character.

Data Structure9 Evaluation & Substitution The brace grouping ({}) set Z Albany set Z_LABEL "The Capitol of New York is: " puts "\n examples of differences between \" and \{" puts "$Z_LABEL $Z" puts {$Z_LABEL $Z} puts "\n examples of differences in nesting \{ and \" " puts "$Z_LABEL {$Z}" puts {Who said, "What this country needs is $0.05 cigar and $Z!"?} puts "\n examples of escape strings" puts {There are no substitutions done within braces \n \r \x0a \f \v} puts {But, the escaped newline at the end of a\ string is still evaluated as a space}

Data Structure10 Evaluation & Substitution The square brackets grouping ([]) You obtain the results of a command by placing the command in square brackets. the string within the square brackets is evaluated as a command by the interpreter, and the result of the command replaces the square bracketed string. Exception rules  A square bracket that is escaped with a \ is considered as a literal square bracket.  A square bracket within braces is not modified during the substitution phase.

Data Structure11 Evaluation & Substitution The square brackets grouping ([]) set x abc puts "A simple substitution: $x\n" set y [set x "def"] puts "Remember that set returns the new value of the variable: X: $x Y: $y\n" set z {[set x "This is a string within quotes within braces"]} puts "Note the curly braces: $z\n" set a "[set x {This is a string within braces within quotes}]" puts "See how the set is executed: $a" puts "\$x is: $x\n" set b "\[set y {This is a string within braces within quotes}]" # Note the \ escapes the bracket, # and must be a literal character in double quotes puts "Note the \\ escapes the bracket:\n \$b is: $b" puts "\$y is: $y"

Data Structure12 Math 101 The Tcl command for doing math type operations is “expr”. You should always use braces Math Functions % set userinput {[puts DANGER!]} [puts DANGER!] % expr $userinput == 1 DANGER! 0 % expr {$userinput == 1} 0 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

Data Structure13 Math 101 Operators (in decreasing order of precedence) -, +, ~, ! ** *, /, % +, - >, = eq, ne, in, ni  compare two strings for equality (eq) or inequality (ne). and two operators for checking if a string is contained in a list (in) or not (ni).  the operands are regarded exclusively as strings & ^ | && || x?y:z % expr { "9" == "9.0"} 1 % expr { "9" eq "9.0"} 0 % set x 1 % expr { $x>0? ($x+1) : ($x-1) } 2

Data Structure14 Math 101 Math Example set X 100 set Y 256 set Z [expr {$Y + $X}] set Z_LABEL "$Y plus $X is " puts "$Z_LABEL $Z" puts "The square root of $Y is [expr { sqrt($Y) }]\n" puts "Because of the precedence rules \" * 4\" is: [expr {-3 * 4 + 5}]" puts "Because of the parentheses \"(5 + -3) * 4\" is: [expr {(5 + -3) * 4}]" set A 3 set B 4 puts "The hypotenuse of a triangle: [expr {hypot($A,$B)}]" set pi6 [expr { /6.0}] puts "The sine and cosine of pi/6: [expr {sin($pi6)}] [expr {cos($pi6)}]" set a(1) 10 set a(2) 7 set a(3) 17 set b 2 puts "Sum: [expr {$a(1)+$a($b)}]"

Data Structure15 if Interpretation of Expression Example set x 1 if {$x == 2} {puts "$x is 2"} else {puts "$x is not 2"} if {$x != 1} { puts "$x is != 1" } else { puts "$x is 1" } if $x==1 {puts "GOT 1"} set y x if "$$y != 1" { puts "$$y is != 1" } else { puts "$$y is 1" } if "3 < 2" { puts "Hello1" } set x 1 if "$x < 2" { puts "Hello3" }

Data Structure16 switch Example set x "ONE" set y 1 set z ONE switch $x { "$z" { set y1 [expr {$y+1}] puts "MATCH \$z. $y + $z is $y1" } ONE { set y1 [expr {$y+1}] puts "MATCH ONE. $y + one is $y1" } TWO { set y1 [expr {$y+2}] puts "MATCH TWO. $y + two is $y1" } default { puts "$x is NOT A MATCH" } switch $x "ONE" "puts ONE=1" "TWO" "puts TWO=2" "default" "puts NO_MATCH"

Data Structure17 while Example set x 1 while {$x < 5} { puts "x is $x" set x [expr {$x + 1}] } puts "exited first loop with X equal to $x\n“ set x 0 while "$x < 5" { set x [expr {$x + 1}] if {$x > 7} break if "$x > 3" continue puts "x is $x" } puts "exited second loop with X equal to $x"

Data Structure18 for Example for {set i 0} {$i < 10} {incr i} { puts "I inside first loop: $i" } for {set i 3} {$i < 2} {incr i} { puts "I inside second loop: $i" } puts "Start" set i 0 while {$i < 10} { puts "I inside first loop: $i" incr i puts "I after incr: $i" } set i 0 incr i # This is equivalent to: set i [expr {$i + 1}]