1
1
while {$i<=$list_size} {
#count odd numbers from a list puts [lindex $list $i]
incr i
set list "1 3 4 5 6 2 59 20 34 33 45 65 66 21 3" }
foreach data $list {
set i 0 puts $data
foreach number $list { }
if {$number%2 != 0} {
lappend odd_nums
$number
incr i 4. #Write a TCL script to sort a list without using
} the lsort function.
}
puts "Total Odd numbers: $i" set list "80 90 100 10 50 3"
puts "Odd numbers are: $odd_nums"; puts "The list is: $list"
2. set temp {}
#Make a program that will repeatedly ask the user set temp1 {}
to guess a secret number from 1 to 100 until the set list1 $list
user guesses the secret number. set list_size [llength $list]
#Your program should pick the number at random set list_size_1 [expr {$list_size - 1}]
by using the magical formula int(1 + rand 100). for {set i 0} { $i <$list_size} {incr i} {
When the user guesses wrong, for {set j 0} { $j <$list_size_1} {incr j} {
#the program should respond “Too high” or “Too set x [expr {$j + 1}]
low.” If the user enters the word quit or exit, or if if {[lindex $list $j] > [lindex $list
the user enters a blank line, $x]} {
#the program should quit. Of course, if the user set temp [lindex $list
guesses correctly, the program should quit then as $x]
well! lset list $x [lindex $list
$j]
while {1} { lset list $j $temp
puts "Guess a secret number from 1 to 100: " set list_ascending $list
gets stdin user_ip }
}
set random [expr {1+ int(rand()*100)}] for {set k 0} { $k <$list_size_1} {incr k}
if {[regexp -nocase {quit|exit} $user_ip]|[regexp {
{^$} $user_ip]} { set y [expr {$k + 1}]
exit; if {[lindex $list1 $k] < [lindex
} elseif {$user_ip>=0 & $user_ip<=100} { $list1 $y]} {
if {$user_ip==$random} { set temp1 [lindex $list1
puts "You guessed it $y]
right." lset list1 $y [lindex
exit; $list1 $k]
} elseif {$user_ip<$random} { lset list1 $k $temp1
puts "Input is Too Low." set list_descending
} else { $list1
puts "Input is Too High." }
} }
} else { }
puts "Wrong input!" puts "Ascending order: $list_ascending";
} puts "Descending order: $list_descending";
}
puts "Enter the file name along with the path:" set fh [open $in r]
gets stdin path while {[gets $fh data]>=0} {
lappend content $data
set out_path "d:/palindromes.txt" }
#Recursive implementation
#proc fact {n} { set dirs [glob -type d $in/*]
# if { $n == 0 } { set file_names [glob -type f $in/*.v]
# return 1
# } else { puts "Name of the Directories are:"
# return [expr {$n*[fact [expr {$n-1}]]}] foreach data_dir $dirs {
# } puts $data_dir
#} }
puts "\nName of the Files are:"
foreach data_file $file_names {
proc avg {} { puts $data_file
puts "How many numbers you want to }
add:"
gets stdin no 15.
set result 0 #Write a TCL script so that when it is executed, it
for {set i 1} {$i<=$no} {incr i} { should get the information like names and marks of
puts "Enter a number:" "N" number of students from the user through
gets stdin value standard input.
set result [expr {$result + #Value of N should be accessed through command
$value}] line argument. Make an associative array in TCL
set average [expr {$result/$no}] that has a list of all the names of the students and
} their marks.
puts "Result= $average" #Also write a subroutine so that it calculates the
} avg marks of all entire associative array.
puts "Enter '1' for Factorial of a number and '2' for proc average {array} {
calculate avg of numbers:" set avg 0
gets stdin in upvar $array a
set x [array size a]
if {$in==1} { foreach key [array names a] {
puts "Enter a number:" puts "Name:${key}
gets stdin fact_in Marks:$a($key)"
set result [fact $fact_in] set avg [expr {($avg +
puts "Factorial of $fact_in is $result." $a($key))}]
} elseif {$in==2} { }
avg puts "Avg marks of $x students: [expr
} else { {$avg/$x}]"
puts "Wrong Input!!" }
}
set n [lindex $argv 0]
for {set i 0} {$i<$n} {incr i} {
set x [expr {$i + 1}]
#fact puts "Enter no.$x student's name:"
gets stdin name_ip
#set result [expr {$a * $b * $c}] puts "Enter no.$x student's marks:"
#for {set i $size} {$i>=0} {incr i -1} { gets stdin msrks_ip
set names($name_ip) $msrks_ip
14. }
#Write a script which when executed will check array set mynewarr [average names]
wheather each file in a particular directory is a file
or directory.
#Also sort the files as well as directories in separate 16.
list and finally display the list of files as well as the #Write a subroutine which can take numeric value
#list of directories available in that directory to the from 1 to 9 as an argument and return its number
user. such 'one', 'two' etc. as it returns value.
#If the value is out of range, then return the original
#argv Syntax example: tclsh d:/sort_files_dir.tcl number as its return value.
d:/new
proc numeric {value} {
set in [lindex $argv 0] switch "$value" {
"1" {[return "ONE"]}
"2" {[return "TWO"]} #functions on lists. Display the output on the screen
"3" {[return "THREE"]} after every operation.
"4" {[return "FOUR"]}
"5" {[return "FIVE"]} set list {Arka Nikki Dk}
"6" {[return "SIX"]} set list1 {80 20 67}
"7" {[return "SEVEN"]} set list2 {Vellore TN}
"8" {[return "EIGHT"]} puts "$list"
"9" {[return "NINE"]} #o/p: Arka Nikki Dk
"default" {[return $value]}
} lappend list "Tathagata"
} puts "$list"
#o/p: Arka Nikki Dk Tathagata
puts "Please enter any Number between 1-9: "
gets stdin in lset list 2 "VIT"
puts "$list"
puts "$in = [numeric $in]" #o/p: Arka Nikki VIT Tathagata
puts "Please enter the Source File path: " set list [lreplace $list 0 2 80 20 67]
gets stdin in puts "$list"
puts "Please enter the Output File's Name (With #o/p: 80 20 67
Extension): "
gets stdin out_name set list [linsert $list 3 90]
puts "$list"
set fo [open $out_name w] #Syntax: linsert list index element1 ?element2
set fi [open $in r] element3 ...
while { [gets $fi data] >= 0 } { #o/p: 80 20 67 90
puts $fo $data
} set length [llength $list]
close $fo puts "$length"
close $fi #Syntax: llength list
puts "$out_name is created successfuly."; #o/p: 4
puts "Enter the upper limit: " set result [lsearch -inline $list "90"]
gets stdin max puts "$result"
puts "Enter the lower limit: " #o/p: 90 (True- The value, False- an empty string)
gets stdin min
#[-all, -ascii, -decreasing, -increasing, -exact, -glob,
for {set i 0} {$i<=10} {incr i} { -increasing, -integer, -not, -real, -regexp, -sorted, -
set random_number [expr {$min + start index]
int(rand()*($max - $min))}]
puts "Random number is: puts $list
$random_number"
} set list [lset list 1 Nikki]
puts "$list"
19. #This will change an element in a list
#Write a TCL script to show the operations of #Syntax: lset varName ?index...? newValue..
lappend, lset, lreplace, linsert, #o/p: 80 Nikki 67 90
#llength, lsearch, lset, lrepeat, lsort, lreverse, split,
join, concat, range puts [lrepeat 3 a]
# Build a list by repeating elements
#Syntax: lrepeat count ?element ...
#o/p: a a a 20.
#Write a TCL program that reads the list of RTL
set a [lrepeat 3 [lrepeat 3 a b c]] and test bench files (Verilog files) in a design and
#o/p: a b c a b c a b c then compiles all the files
#using modelsim simulator and if compilation is
set list [lsort $list] successful for a design, then simulate the design
puts $list using the same simulator.
#Syntax: lsort ?options? list #Print messages such as “TEST PASSED― on
#o/p: 67 80 90 Nikki successful simulation and “TEST FAILED― if
#[-ascii, -dictionary, -integer, -real, -increasing, - the simulation is unsuccessful also if
decreasing, -indices, -index indexList] #compilation is unsuccessful, then print the
message as “check the log file for compilation
set list [lreverse $list] errors―.
puts $list
#Syntax: lreverse list puts "Enter the directory path containing .v files: ";
#o/p: Nikki 67 80 90 gets stdin in
set string [join $list " "] set v_files [glob -type f $in/*.v]
puts $string set v_size [llength $v_files]
#list to string puts $v_size
#o/p: Nikki 67 80 90 (string) set log_path "d:/log.txt"
if {[regexp {[05]$} $length]} { # h) Swap the two words of the following string
puts "Length of \"$text3\" is $length, and "45 67".
its multiple of 5."
} else { set text8 "45 67"
puts "Length of \"$text3\" is $length, and regexp {^(\d+)(\s)(\d+)} $text8 _ a b c
its not multiple of 5." set temp [concat $c$b$a]
}
puts "Old String: $text8, Reversed String: $temp"
# d) Any four digit number that reads the same
backwards as forwards (like 4114)
# i) http://www.beedub.com:80/index.html
set text4 "4114"
regexp {(\d)(\d)(\d)(\d)} $text4 _ a b c d set url http://www.beedub.com:80/index.html
set temp [concat $a$b$c$d]
if {[regexp
if {$text4 eq $temp} { {^(\w+\://\w+\.\w+\.\w+\:\d+\/\w+\.\w+)$} $url _
puts "$text4 is a palindrome number." a]} {
} else { puts "$a."
puts "$text4 is not a palindrome number." } else {
} puts "URL is not matching."
}
# e) A number in Roman numerals regexp {^(\w+)\:} $url _ b
regexp {^\w+\://(\w+\.\w+\.\w+)\:\d+\/\w+\.\w+$}
set text5 "XC" $url _ c
puts "Roman numeral: $text5" regexp {^\w+\://\w+\.\w+\.\w+(\:\d+)\/\w+\.\w+$}
$url _ d
if {[regexp regexp {^\w+\://\w+\.\w+\.\w+\:\d+\/(\w+\.\w+)$}
{^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(I $url _ e
X|IV|V?I{0,3})$} $text5]} { puts $b
puts "$text5 is a number in Roman puts $c
numerals." puts $d
} else { puts $e
puts "Check \"$in2\" for Reversed file."
# j) match the following words a507, b702, c334.
set students {A B C D E F G H I J}
puts $students
29.
#Write a TCL programme that creates the skeleton set students_new [linsert $students 0 $students]
of a test suite such taht the directory steucture puts $students_new
should be as in fig. 1. Also assume you need to
copy two different #.v files lappend students_new $students
#residing in the current working directory to test puts $students_new
each test directory. Prompt the user to enter the
number of test directories to be created through the puts "Enter a number(0-10): "
standard input. gets stdin no
#Pass the names of the #.v files that need to be
copied to each test directory as the command line if {$no<10 && $no>=0} {
argument. puts "Value at $no index no: [lindex
$students $no]"
set cur_dir [pwd] } else {
puts "Wrong ip"
set temp "/" }
set in_file_1 [concat $cur_dir$temp[lindex $argv
0]] set students [lreplace $students [expr {[llength
set in_file_2 [concat $cur_dir$temp[lindex $argv $students]-1}] [expr {[llength $students]-1}]]
1]]
puts "Removed last element: $students"
set temp1 "/Testcases/"
set dir [concat $cur_dir$temp1] set students [lreplace $students 0 0]
file mkdir $dir puts "Removed starting element: $students"
puts "List of the Cell instances of the given verilog set fp [open $v_name r]
netlist code:" while { [gets $fp data] >= 0 } {
puts $fo "List of the Cell instances of the given lappend text $data
verilog netlist code:" if {[regexp {^\s*(\w+)\s\w+\s?\(\s?\.\w+} $data
for {set j 0} { $j <$uniqueList_size} {incr j} { _ a]} {
set count 0 lappend instance_names $a
for {set i 0} { $i <$instance_size} {incr i} }
{ }
if {[lindex $uniqueList $j] eq close $fp
[lindex $instance_names $i]} {
incr count set uniqueList [lsort -unique $instance_names]
}
} set uniqueList_size [llength $uniqueList]
puts "[lindex $uniqueList $j]: $count" set instance_size [llength $instance_names]
puts $fo "[lindex $uniqueList $j]: $count"
} puts "List of the Cell instances of the given verilog
close $fo netlist code:"
set no 0
puts "$netlist_name is created successfully.\n" for {set j 0} { $j <$uniqueList_size} {incr j} {
set count 0
puts "Patching work is required or not 'yes' or 'no':
" for {set i 0} { $i <$instance_size} {incr i}
gets stdin user_ip {
if {[lindex $uniqueList $j] eq
if {[regexp {YES|yes|Yes|Y|y} $user_ip]} { [lindex $instance_names $i]} {
puts "Enter the name of the cell instance incr count
which you want to patch: "
gets stdin patched_data }
puts "Enter New name of cell instance: " }
lappend no $count
puts "[lindex $uniqueList $j]: $count" set error_mgs {}
} set count_error 0
set content_size [llength $content]
set sorted [lsort -integer $no] for {set j 0} { $j <$content_size} {incr j}
set length [expr {[llength $sorted]-1}] {
set max_location [lsearch $no [lindex $sorted set content_line [lindex $content
$length]] $j]
set max_value [lindex $uniqueList [expr if {[regexp {TEST PASSED}
{$max_location - 1}]] $content_line]} {
set status "PASS"
puts "\nCell that occur for maximun number of }
times i.e. [lindex $no $max_location] is \"[lindex if {[regexp {TEST FAILED}
$uniqueList [expr {$max_location - 1}]]\"\n"; $content_line]} {
puts "Enter a new cell ame to replace with set status "FAIL"
\"$max_value\": " }
gets stdin new_name if {[regexp {^\s*ERROR\s-
set uniqueList [lreplace $uniqueList [expr \s(.*)} $content_line _ a]} {
{$max_location - 1}] [expr {$max_location - 1}] lappend error_mgs $a
$new_name] incr count_error
set size_list [llength $uniqueList] }
for {set k 0} {$k<=[expr {$size_list}]} {incr k} { }
puts "[lindex $uniqueList [expr {$k -2}]]: set content {}
[lindex $no [expr {$k-1}]]" set count_error 0
} puts $fo "$x\t$name\t$status\t[lindex
$error_mgs 0]";
set error_mgs_size [llength $error_mgs]
33. if {$error_mgs_size>1} {
for {set k 1} { $k
#Write a TCL script that reads the given set of log <$error_mgs_size} {incr k} {
files from different simulations and generates a puts $fo "\t\t\t[lindex
consolidated report in .xls $error_mgs $k]";
#format which should contain the information as }
below. If the test is successful, the log file has the }
status as “TEST PASSED― and }
#if the test is unsuccessful, then the log file has the puts "Please check \"report.xls\" for report."
status as “TEST FAILED―. close $fo
set temp_f [expr {32 + (9 * $temp_c)/5}] #Write a program that creates a file called
puts "Temperature in Fahrenheit: $temp_f\F" "data.dat" in the current directory. Prompt the user
for five numbers, and write them, one at a time, on
both the foreach i {one two three} item {car coins rocks} {
#screen and into the file. Close the file, then open it puts "$i $item"
again for reading only, and display the contents on }
the screen.
#o/p
set cur_dir [pwd] #one car
#two coins
set temp "/" #three rocks
set file_name "data.dat"
set path [concat $cur_dir$temp$file_name]
set s1 "Hello"
set fo [open $path w] set s2 "World"
puts "Enter 5 numbers:" set s3 "World"
for {set i 1} {$i<=5} {incr i} {
puts "Enter no. $i number:" puts [string compare $s1 $s2]
gets stdin no if {[string compare $s2 $s3] == 0} {
puts $fo $no puts "String \'s1\' and \'s2\' are same.";
puts "Entered number: $no" }
}
close $fo if {[string compare $s1 $s2] == -1} {
puts "String \'s1\' comes before \'s2\'";
set fi [open $path r] }
while { [gets $fi data] >= 0 } {
lappend content $data if {[string compare $s2 $s1] == 1} {
} puts "String \'s2\' comes before \'s1\'";
puts "The contents of the file:" }
puts "$content"
close $fi #o/p
#String 's1' and 's2' are same. [s1 should be s3]
39. #String 's1' comes before 's2'
#String 's2' comes before 's1'
puts [linsert {1 2} 0 new stuff]
set x [list a {b c} e d]
puts $x proc parray name {
upvar $name a
#o/p
#new stuff 1 2 foreach el [lsort [array names a]] {
#a {b c} e d puts "$el = $a($el)"
}
puts [lreplace $x 1 2 B C] }
#o/p #o/p
#{b c} e d #age = 37
#name = John
puts [lsearch {here is a list} 1*] #position = Vice President
set list "My name is Tathagata. \nThis is VIT. \nI set data
am doing m.Tech." "ccccccccaaabbbbaaaabbinformationbcaaaaaabbbb
bbbccbb"
puts "Enter a number:"
gets stdin in regsub -all {[a-c]*} $data "" data
47. 50.
#Assume a file named a.txt contains a sequence of
#Create a new TCL command called "ldelete" numbers separated by comma
which deletes a particular element #as 12 / 12 / 29 / 30 / 20. Write a TCL script so that
#in a list, It takes two arguments a list and a value, the file a.txt should
it deletes the element #be modified as,
#specified by the value.
#12
set new [expr {$new + $no}]
#13 set final [format %c $new]
puts $final
#29
53.
#30
#A directory named "test" contains 100
#20 subdirectories of different tests.
#Each testcase has 5 files and one of these files,
named "monitor_cmd.v"
set fi [open "d:/new.txt" r] #needs to be deleted. Wrote a TCL script to
while {[gets $fi data]>=0} { perform the above said task.
lappend content $data
} set path "d:/new"
close $fi
set dirs [glob -type d $path/*]
set fo [open "d:/new.txt" w] set size [llength $dirs]
set list [split $content "/"] for {set i 0} {$i<$size} {incr i} {
foreach temp $list { set temp [lindex $dirs $i]
puts "$temp\n" cd $temp
puts $fo "$temp\n" set file_names [glob -type f $temp/*]
} set result [lsearch -all $file_names
close $fo *monitor_cmd.v*]
puts $file_names
puts $result
51. file delete [lindex $file_names $result]
}
#Write a TCL script to find the number of
occurrence of a cell named "DFFRX1"
#in a netlist. The netlist file should be passed as 54.
command line argument while
#executing the script. #Write a TCL script so that when it is executed, it
prompts the user to
set v_name [lindex $argv 0] #enter the names of two files and then identifies
whether those files
set fp [open $v_name r] #are identical or not. Display the appropriate
while { [gets $fp data] >= 0 } { messages to the user.
lappend text $data
} puts "Enter 1st file's name:"
close $fp gets stdin path1
puts "Enter 2nd file's name:"
set count 0 gets stdin path2
foreach temp $text {
if {[regexp set fp1 [open $path1 r]
{^\s*ad01d0\s\w+\s?\(\s?\.\w+} $temp]} { while {[gets $fp1 content1]>=0} {
incr count lappend data1 $content1
} }
} close $fp1
puts $count
set fp2 [open $path2 r]
52. while {[gets $fp2 content2]>=0} {
lappend data2 $content2
#Write a script to increment charter. }
close $fp2
puts "Enter a Charter:"
gets stdin in if {$data1 eq $data2} {
puts "Enter a integer to increment:" puts "Both files are identical"
gets stdin no } else {
puts "Both files are not identical"
set new [scan $in %c] }
array set names [list delhi 110004 chennai 600001
55. mumbai 400011 kolkata 700001 bangalore 530068]
#Write a TCL script to generate all the possible foreach {key value} [array get names] {
random input data patterns puts "$key => $value"
#to verify a 16 bit adder. }
puts "How many set of data you want to generate?" puts "Enter any one place name for pin code:"
gets stdin in_set gets stdin place
set names {A B}
set in_no [llength $names] set count 0
set bit 16 foreach {key value} [array get names] {
if {$key == $place} {
puts "\nRandom value are:" puts "$key => $value"
for {set i 0} {$i<$in_set} {incr i} { } else {
set temp [expr {pow(2,$bit)}] incr count
for {set j 0} {$j<$in_no} {incr j} { }
set random [expr {int (rand }
()*$temp)}]
set in_binary [format %b if {$count > 0} {
$random] puts "ERROR: Pin code of $place is not
puts "[lindex $names $j]: available."
$in_binary" }
}
} 58.
#Set up an associative array that contains a few city file mkdir $sub_path
names and their corresponding zip for {set i 0} {$i<$size} {incr i} {
#codes. Make at-least 5 elements in the array. set temp [lindex $dirs $i]
Display the names of the cities and ask cd $temp
#the user to enter one. Display the corresponding set file_names [glob -type f $temp/*.txt]
zip code if a city entered by the user set size1 [llength $file_names]
#is in the array. If the city is not in the array then for {set j 0} {$j<$size1} {incr j} {
generate an error message and print set temp1 [lindex $file_names $j]
#the error message file copy -force $temp1
$sub_path
#set names(110004) delhi }
#set names(600001) chennai }
#set names(400011) mumbai puts "Files copied successfully."
#set names(700001) kolkata
#set names(530068) bangalore
if {$v_size>0} {
exec vlib rtl_work
exec vmap work rtl_work
for {set j 0} {$j<$v_size} {incr j} {
exec {vlog [lindex $v_files $i]
>> $log_path};
}
}
60.
#Error Command