1 / 18

Tcl and Otcl Tutorial Part I

Tcl and Otcl Tutorial Part I. Internet Computing Laboratory @ KUT Youn-Hee Han. Prime Number - Batch Mode. 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

Download Presentation

Tcl and Otcl Tutorial Part I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Prime Number - Batch Mode 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 prime.tcl # tclsh prime 5 Data Structure

  3. 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 Structure

  4. Variable • Typeless variable • Variable declaration needed only to global variable • set command • $ prefix set name “han” puts $name puts “My name is $name” Name = “Jone” ;# Error 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" Data Structure

  5. 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 Structure

  6. 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 Structure

  7. 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 \$100.00 bill" set a 100.00 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 Structure

  8. 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 Structure

  9. 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 Structure

  10. 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 Structure

  11. 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 Structure

  12. 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 Structure

  13. 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 Structure

  14. 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 \"5 + -3 * 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 {3.1415926/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 Structure

  15. 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 Structure

  16. 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 Structure

  17. 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 Structure

  18. 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}] Data Structure

More Related