You are on page 1of 3

Tcl Assignment 3

1. Try out the following command sequence and interpret the output :
proc sum {arg1 arg2} {
set x [expr {$arg1 + $arg2}];
return $x
}

puts " The sum of 2 + 3 is: [sum 2 3]\n\n"

proc for {a b c} {
puts "The for command has been replaced by a puts";
puts "The arguments were: $a\n$b\n$c\n"
}

for {set i 1} {$i < 10} {incr i}

2. Try out the following command sequence and interpret the output:

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"
}
}
}

set count1 [example ONE]


set count2 [example ONE TWO]
set count3 [example ONE TWO THREE ]
set count4 [example ONE TWO THREE FOUR]

puts "The example was called with $count1, $count2, $count3, and $count4 Arguments"
3. Try out the following command sequence and interpret the output :

set x "a b c"


puts "Item at index 2 of the list {$x} is: [lindex $x 2]\n"

set y [split 7/4/1776 "/"]


puts "We celebrate on the [lindex $y 1]'th day of the [lindex $y 0]'th month\n"

set z [list puts "arg 2 is $y" ]


puts "A command resembles: $z\n"

set i 0
foreach j $x {
puts "$j is item number $i in list x"
incr i
}

4. Try out the following command sequence and interpret the output :

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"

5. Try out the following command sequence and interpret the output :

set b [list a b {c d e} {f {g h}}]


puts "Treated as a list: $b\n"

set b [split "a b {c d e} {f {g h}}"]


puts "Transformed by split: $b\n"

set a [concat a b {c d e} {f {g h}}]


puts "Concated: $a\n"

lappend a {ij K lm} ;# Note: {ij K lm} is a single element


puts "After lappending: $a\n"

set b [linsert $a 3 "1 2 3"] ;# "1 2 3" is a single element


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"

6. Write a program to fill up an array with random values from 1 to 50000. Then accept a
number from the user and if that number is present in the array display “Number found at
location <Location number>” and exit. If that number is not present in the array display
“Number not found” and take another input from the user and repeat the above process.

You might also like