Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- proc bracess { } {
- set y [set x "def"]
- puts "Remember that set returns the new value of the variable: X: $x Y: $y\n"
- set a "[set x {This is a string within braces within quotes}]"
- puts " x is now $x \n"
- puts " a is now $a \n"
- set b "\\[set y {This is a string within braces within quotes}]"
- # Note the \ escapes the bracket, and must be doubled to be a
- # literal character in double quotes
- puts "Note the \\ escapes the bracket:\n \$b is: $b"
- puts "\$y is: $y"
- }
- proc expres { } {
- 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"
- set pi6 [expr {3.1415926/6.0}]
- puts "The sine and cosine of pi/6: [expr {sin($pi6)}] [expr {cos($pi6)}]"
- # Working with arrays
- set a(1) 10
- set a(2) 7
- set a(3) 17
- set b 2
- puts "Sum: [expr {$a(1)+$a($b)}]"
- puts "1/2 is [expr {1/2}]"
- puts "-1/2 is [expr {-1/2}]"
- puts "1./2 is [expr {1./2}]"
- puts "1./3 is [expr {1./3}]"
- puts "d1/3 is [expr {double(1)/3}]"
- }
- proc example {first {second ""} args} {
- if {$second eq ""} {
- puts "There is only one argument and it is: $first"
- return 1
- } else {
- if {$args eq ""} {
- puts "There are two arguments - $first and $second"
- return 2
- } else {
- puts "There are many arguments - $first and $second and $args"
- return "many"
- }
- }
- }
- proc SetPositive {variable value } {
- upvar $variable myvar
- if {$value < 0} {
- set myvar [expr {-$value}]
- } else {
- set myvar $value
- }
- return $myvar
- }
- proc existence {variable} {
- upvar $variable testVar
- if { [info exists testVar] } {
- puts "$variable Exists"
- } else {
- puts "$variable Does Not Exist"
- }
- }
- proc do_existence { } {
- set x 1
- set y 2
- for {set i 0} {$i < 5} {incr i} {
- set a($i) $i;
- }
- existence x
- unset x
- existence x
- }
- proc listi { } {
- set x "a b c"
- set y [split 7/4/1776 "/"]
- set z [list puts "arg 2 is $y" ]
- set lst [list "item 1" "item 2" "item 3"]
- puts $x
- puts $y
- puts lst
- puts $lst
- set i 0
- foreach j $x {
- puts "$j is item number $i in list x"
- incr i
- }
- set b [list a b {c d e} {f {g h}}]
- puts "Treated as a list: $b\n"
- set a [concat a b {c d e} {f {g h}}]
- puts "Concated: $a\n"
- lappend a {ij Klm}
- puts "After lappending: $a\n"
- set b [linsert $a 3 "1 2 3"]
- puts "After linsert at position 3: $b\n"
- set b [lreplace $b 3 5 "AA" "BB"]
- puts "After lreplacing 3 positions with 2 values at position 3: $b\n"
- }
- proc misterPresidentList { } {
- set list [list {Washington 1789} {Adams 1797} {Jefferson 1801} {Madison 1809} {Monroe 1817} {Adams 1825} ]
- set x [lsearch $list Washington*]
- set y [lsearch $list Madison*]
- incr x
- incr y -1 ;# Set range to be not-inclusive
- set subsetlist [lrange $list $x $y]
- puts "The following presidents served between Washington and Madison"
- foreach item $subsetlist {
- puts "Starting in [lindex $item 1]: President [lindex $item 0] "
- }
- set x [lsearch $list Madison*]
- set srtlist [lsort $list]
- set y [lsearch $srtlist Madison*]
- puts "\n$x Presidents came before Madison chronologically"
- puts "$y Presidents came before Madison alphabetically"
- }
- #abc - cba
- proc revert { str } {
- set strLength [string length $str]
- set bound [expr {($strLength - 1) / 2 }]
- set strList [split $str ""]
- for { set i 0 } { $i <= $bound } { incr i } {
- set temp [lindex $strList $i]
- lset strList $i [lindex $strList [expr {$strLength - 1 - $i}]]
- lset strList [expr {$strLength - 1 - $i }] $temp
- }
- set str [join $strList ""]
- return $str
- }
- puts "\n\n\n"
- set str "elephant fly slowly"
- puts [revert $str]
- puts "\n\n\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement