Advertisement
Guest User

Untitled

a guest
Aug 15th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 3.82 KB | None | 0 0
  1. proc bracess { } {
  2.     set y [set x "def"]
  3.     puts "Remember that set returns the new value of the variable: X: $x Y: $y\n"
  4.  
  5.     set a "[set x {This is a string within braces within quotes}]"
  6.     puts " x is now $x \n"
  7.     puts " a is now $a \n"
  8.  
  9.     set b "\\[set y {This is a string within braces within quotes}]"
  10.     # Note the \ escapes the bracket, and must be doubled to be a
  11.     # literal character in double quotes
  12.  
  13.     puts "Note the \\ escapes the bracket:\n \$b is: $b"
  14.     puts "\$y is: $y"
  15. }
  16.  
  17. proc expres { } {
  18.     set X 100
  19.     set Y 256
  20.     set Z [expr {$Y + $X}]
  21.     set Z_LABEL "$Y plus $X is "
  22.     # puts "$Z_LABEL $Z"
  23.     # puts "The square root of $Y is [expr "sqrt($Y)"]\n"
  24.  
  25.     set pi6 [expr {3.1415926/6.0}]
  26.     puts "The sine and cosine of pi/6: [expr {sin($pi6)}] [expr {cos($pi6)}]"
  27.  
  28.     # Working with arrays
  29.  
  30.     set a(1) 10
  31.     set a(2) 7
  32.     set a(3) 17
  33.     set b    2
  34.     puts "Sum: [expr {$a(1)+$a($b)}]"  
  35.    
  36.     puts "1/2 is [expr {1/2}]"
  37.     puts "-1/2 is [expr {-1/2}]"
  38.     puts "1./2 is [expr {1./2}]"
  39.     puts "1./3 is [expr {1./3}]"
  40.     puts "d1/3 is [expr {double(1)/3}]"
  41. }
  42.  
  43. proc example {first {second ""} args} {
  44.     if {$second eq ""} {
  45.         puts "There is only one argument and it is: $first"
  46.         return 1
  47.     } else {
  48.         if {$args eq ""} {
  49.             puts "There are two arguments - $first and $second"
  50.             return 2
  51.         } else {
  52.             puts "There are many arguments - $first and $second and $args"
  53.             return "many"
  54.         }
  55.     }
  56. }
  57.  
  58. proc SetPositive {variable value } {
  59.     upvar $variable myvar
  60.     if {$value < 0} {
  61.         set myvar [expr {-$value}]
  62.     } else {
  63.         set myvar $value
  64.     }
  65.     return $myvar
  66. }
  67.  
  68. proc existence {variable} {
  69.     upvar $variable testVar
  70.     if { [info exists testVar] } {
  71.         puts "$variable Exists"
  72.     } else {
  73.         puts "$variable Does Not Exist"
  74.     }
  75. }
  76. proc do_existence { } {
  77.     set x 1
  78.     set y 2
  79.     for {set i 0} {$i < 5} {incr i} {
  80.         set a($i) $i;
  81.     }
  82.     existence x
  83.     unset x
  84.     existence x
  85. }
  86.  
  87. proc listi { } {
  88.     set x "a b c"
  89.     set y [split 7/4/1776 "/"]
  90.     set z [list puts "arg 2 is $y" ]
  91.     set lst [list "item 1" "item 2" "item 3"]
  92.  
  93.     puts $x
  94.     puts $y
  95.     puts lst
  96.     puts $lst
  97.  
  98.     set i 0
  99.     foreach j $x {
  100.         puts "$j is item number $i in list x"
  101.         incr i
  102.     }
  103.  
  104.     set b [list a b {c d e} {f {g h}}]
  105.     puts "Treated as a list: $b\n"
  106.  
  107.     set a [concat a b {c d e}           {f {g h}}]
  108.     puts "Concated: $a\n"
  109.  
  110.     lappend a {ij Klm}
  111.     puts "After lappending: $a\n"
  112.  
  113.     set b [linsert $a 3 "1 2 3"]
  114.     puts "After linsert at position 3: $b\n"
  115.  
  116.     set b [lreplace $b 3 5 "AA" "BB"]
  117.     puts "After lreplacing 3 positions with 2 values at position 3: $b\n"
  118. }
  119.  
  120. proc misterPresidentList { } {
  121.     set list [list {Washington 1789} {Adams 1797} {Jefferson 1801} {Madison 1809} {Monroe 1817} {Adams 1825} ]
  122.  
  123.     set x [lsearch $list Washington*]
  124.     set y [lsearch $list Madison*]
  125.     incr x
  126.     incr y -1                        ;# Set range to be not-inclusive
  127.  
  128.     set subsetlist [lrange $list $x $y]
  129.  
  130.     puts "The following presidents served between Washington and Madison"
  131.     foreach item $subsetlist {
  132.         puts "Starting in [lindex $item 1]: President [lindex $item 0] "
  133.     }
  134.  
  135.     set x [lsearch $list Madison*]
  136.  
  137.     set srtlist [lsort $list]
  138.     set y [lsearch $srtlist Madison*]
  139.  
  140.     puts "\n$x Presidents came before Madison chronologically"
  141.     puts "$y Presidents came before Madison alphabetically"
  142. }
  143.  
  144. #abc - cba
  145. proc revert { str } {
  146.     set strLength [string length $str]
  147.     set bound [expr {($strLength - 1) / 2 }]
  148.     set strList [split $str ""]
  149.  
  150.     for { set i 0 } { $i <= $bound } { incr i } {
  151.         set temp [lindex $strList $i]
  152.         lset strList $i [lindex $strList [expr {$strLength - 1 - $i}]]
  153.         lset strList [expr {$strLength - 1 - $i }] $temp
  154.     }
  155.     set str [join $strList ""]
  156.     return $str
  157. }
  158.  
  159. puts "\n\n\n"
  160. set str "elephant fly slowly"
  161. puts [revert $str]
  162. puts "\n\n\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement