You are on page 1of 5

Tcl Assignment 4

1. Try out the following Tcl commands and interpret the output :
proc SetPositive {variable value } {
upvar $variable myvar
if {$value < 0} {
set myvar [expr {-$value}]
} else {
set myvar $value
}
return $myvar
}

SetPositive x 5
SetPositive y -5

puts "X : $x Y: $y\n"

proc two {y} {


upvar 1 $y z ;# tie the calling value to variable
z
upvar 2 x a ;# Tie variable x two levels up to a
puts "two: Z: $z A: $a" ;# Output the values, just to confirm
set z 1 ;# Set z, the passed variable to 1;
set a 2 ;# Set x, two layers up to 2;
}

proc one {y} {


upvar $y z ;# This ties the calling value to
variable z
puts "one: Z: $z" ;# Output that value, to check it is
5
two z ;# call proc two, which will change
the value
}

one y ;# Call one, and output X and Y after


the call.
puts "\nX: $x Y: $y"

proc existence {variable} {


upvar $variable testVar
if { [info exists testVar] } {
puts "$variable Exists"
} else {
puts "$variable Does Not Exist"
}
}

set x 1
set y 2
for {set i 0} {$i < 5} {incr i} {
set a($i) $i;
}
puts "\ntesting unsetting a simple variable"
# Confirm that x exists.
existence x
# Unset x
unset x
puts "x has been unset"
# Confirm that x no longer exists.
existence x

2. Try out the following Tcl commands and interpret the output :

# Matches
string match f* foo

# Matches
string match f?? foo

# Doesn't match
string match f foo

# Returns a big list of files on my Debian system.


set bins [glob /usr/bin/*]

3. Try out the following Tcl Commands and interpret the output :

set string "this is my test string"

puts "There are [string length $string] characters in \"$string\""

puts "[string index $string 1] is the second character in \"$string\""

puts "\"[string range $string 5 10]\" are characters between the 5'th and
10'th"
4. Try out the following Tcl commands and interpret the output :

set fullpath "/usr/home/clif/TCL_STUFF/TclTutor/Lsn.17"

set relativepath "CVS/Entries"

set directorypath "/usr/bin/"

set paths [list $fullpath $relativepath $directorypath]

foreach path $paths {

set first [string first "/" $path]

set last [string last "/" $path]

# Report whether path is absolute or relative

if {$first != 0} {

puts "$path is a relative path"

} else {

puts "$path is an absolute path"

# If "/" is not the last character in $path, report the last word.

# else, remove the last "/", and find the next to last "/", and

# report the last word.

incr last

if {$last != [string length $path]} {

set name [string range $path $last end]

puts "The file referenced in $path is $name"

} else {

incr last -2;

set tmp [string range $path 0 $last]


set last [string last "/" $tmp]

incr last;

set name [string range $tmp $last end]

puts "The final directory in $path is $name"

# CVS is a directory created by the CVS source code control system.

if {[string match "*CVS*" $path]} {

puts "$path is part of the source code control tree"

# Compare to "a" to determine whether the first char is upper or lower


case

set comparison [string compare $name "a"]

if {$comparison >= 0} {

puts "$name starts with a lowercase letter\n"

} else {

puts "$name starts with an uppercase letter\n"

5. Try out the following Tcl Commands and interpret the output :

set upper "THIS IS A STRING IN UPPER CASE LETTERS"

set lower "this is a string in lower case letters"

set trailer "This string has trailing dots ...."

set leader "....This string has leading dots"

set both "((this string is nested in parens )))"


puts "tolower converts this: $upper"

puts " to this: [string tolower $upper]\n"

puts "toupper converts this: $lower"

puts " to this: [string toupper $lower]\n"

puts "trimright converts this: $trailer"

puts " to this: [string trimright $trailer .]\n"

puts "trimleft converts this: $leader"

puts " to this: [string trimleft $leader .]\n"

puts "trim converts this: $both"

puts " to this: [string trim $both "()"]\n"

set labels [format "%-20s %+10s " "Item" "Cost"]

set price1 [format "%-20s %10d Cents Each" "Tomatoes" "30"]

set price2 [format "%-20s %10d Cents Each" "Peppers" "20"]

set price3 [format "%-20s %10d Cents Each" "Onions" "10"]

set price4 [format "%-20s %10.2f per Lb." "Steak" "3.59997"]

puts "\n Example of format:\n"

puts "$labels"

puts "$price1"

puts "$price2"

puts "$price3"

puts "$price4"

You might also like