50% found this document useful (2 votes)
723 views

TCL Questions

This document contains examples of TCL scripts and procedures: 1. It shows how to increment each element in a list using a for loop. 2. It defines a sample procedure that iterates from 1 to 5, calling a body within each iteration while manipulating variables. 3. It provides examples of TCL programming concepts like uplevel, upvar, regular expressions, and procedures.

Uploaded by

kchockal1
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
723 views

TCL Questions

This document contains examples of TCL scripts and procedures: 1. It shows how to increment each element in a list using a for loop. 2. It defines a sample procedure that iterates from 1 to 5, calling a body within each iteration while manipulating variables. 3. It provides examples of TCL programming concepts like uplevel, upvar, regular expressions, and procedures.

Uploaded by

kchockal1
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

set y [set x 0][incr x][incr x]

set var 192.168.4.11.234


if { [regexp {^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-
9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){2,4}} $ip] } {
puts �$var valid ip� }

proc sample {varname first last body} {


upvar $varname v
for {set v $first} {$v<=$last} {incr v} {
uplevel $body }
}
set y 222
set v { }
sample i 1 5 {
if { $v == 1 } { puts "v eq to $v" }
puts $y
lappend v [expr $i*1]; puts "$v ## $i"
}
puts $v

uplevel
upvar

simple tcl procedure program by giving one default arguments


proc sm { a {b 34} } {
puts [expr $a+$b] }
set var [sm 5 ]
tcl procedure program should not be like below
proc sm { {a 34} b } {
puts [expr $a+$b] }

TCL Scripts examples with sample programs


1.how to increment eacl element in a list ? eg: incrlist {1 2 3} =>2 3 4
set list1 {1 2 3}
set list2 {}
foreach i $list1 {
lappend list2 [expr {$i+1}] }
puts $list2
o/p:
2 3 4
2.How do you find the length of a string without using string length command in TCL
set var �welcome�
set list1 [split $var ""]
foreach i $list1 {
incr len }
puts $len
o/p:
7
3.How to extract �information� from
�ccccccccaaabbbbaaaabbinformationabcaaaaaabbbbbbbccbb�
in tcl using a single command
puts [string trim "ccccccccaaabbbbaaaabbinformationabcaaaaaabbbbbbbccbb" "abc"]
o/p:
information
4.How to Swap 30 & 40 in IP address 192.30.40.1 using TCL script
set var �192.30.40.1?
set list1 [split $var "."]
set list2 [lreplace $list1 1 2 40 30]
set result [join $list2 "."]
puts $result
o/p:
192.40.30.1
5.Set ip address as 10.30.20.1 write a script to replace the 30 with 40
set var �10.30.20.1?
regsub 30 $var 40 result
puts $result
o/p:
10.40.20.1
6.How do you check whether a string is palindrome or not using TCL script
proc palindrome {str} {
set l [string length $str]
set i 0
incr l -1
set flag 0
while {$l>=0} {
set s [string index $str $i]
set e [string index $str $l]
if {$s==$e} { } else {
set flag 1
break }
incr l -1
incr i 1
}
if {$flag ==0} { puts �The given string $str is palindrome� } else {
puts �The given string $str is not palindrome� }
}
palindrome �malayalam�
palindrome �welcome�

a)what is the most challenging task in automation you have done


b)explain greedy and non-greedy in regular expression
c)what is expect
d)how to detect ip address
e)how to generate ip addresses

1. Write a regexp to match an ip address?


2. How do you pick each part of the ip address and place them in separate
variables?
3. What do these signify (. * ? +) in regexp?
4. What is command substitution, variable substitution and backslash substitution?
(very common question)
5. Difference between using curly braces and quotes in TCL?
6. Convert a string into a list.
7. Convert a list into a string.
8. Write a program to reverse a string.
9. Find the length of a string without using �string length� command.
10. What are your strong areas in TCL?(string, list regexp etc..)
11. What is upvar and uplevel in TCL?
12. Write a program to connect to a system and to login using EXPECT script?
1) set i 1; while $i<=10 {incr i}
set i 1;while {$i<=10} {incr i}

2)set i 0
if $i { puts Hi }
set i 1
if $i { puts Hi } Hi
set i -1
if $i { puts Hi } Hi

3)set i 0
if {$i} { puts Hi }
set i 1
if {$i} { puts Hi }
set i -1
if {$i} { puts Hi }

4)set i

set ::myBigFatGloblVariable "hello"

proc myFirstProc { var1 var2 } {


upvar 1 $var1 local
set local [expr $var2 * 3]
}

proc mySecondProc { var2 } {


puts FFFF
puts $::myBigFatGlobalVariable
set $::myBigFatGlobalVariable [expr $var2 * 3]
}

You might also like