Pages

Sunday, January 15, 2012

TCL Stuffs

#regexp to match all email address
\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

#to match any email address
set a "Raj_btech23@rediffmail.com"
if {[regexp -- {^[A-Za-z0-9._-]+@[[A-Za-z0-9.-]+$} $a b]} {
puts $b
} else {
puts fail
}
#puts $a

#match infinite spaces at start and end.
set str "  sjkhf sdhj   "
set rest [regexp {^ +.* +$} $str match]
puts $rest

#regexp to match the ip address
set str 66.70.7.154
regexp "(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})" $str all first second third fourth
puts "$all \n $first \n $second \n $third \n $fourth \n"

## array example
array set temp {del 30 mum 27 pun 29 che 34}
set naw [array names temp]
puts $naw
foreach var $naw {
puts "$var = $temp($var)"
}

op
che del pun mum
che = 34
del = 30
pun = 29
mum = 27

##sum of elements in list
set list { 1 0 5 3 8 4 }
proc order {list} {
set list1 [ lsort $list ]
puts $list1
set list2 [ lsort -decr $list ]
puts $list2
set sum 0
foreach var $list {
set sum [ expr "$sum + $var" ]or foreach var {1 0 5 3 8 4} { set sum [expr $sum + $var]
}
puts "$sum"
}

1.)
set a { 5 9 3 7 1 }
set b { 1 10 2 8 6 }

set c # c should be elements of a and b in ascending order
set d # d should be elements of a and b in descending order

set a {5 9 3 7 1}
set b {1 10 2 8 6}
set c [lsort -integer "$a $b"]
puts $c
set d [lsort -integer -decr "$a $b"]
puts $d

1 1 2 3 5 6 7 8 9 10
10 9 8 7 6 5 3 2 1 1

#call by value
proc print_hello {count} {
for {} {$count>0} {incr count -1} {
puts "hello world"
}
}
set var 2
print_hello $var

hello world
hello world

#call by reference
proc print_hello {count} {
upvar $count v
set v 2
for {} {$v > 0} {incr v -1} {
puts "hello world"
}
}
print_hello var

hello world
hello world


cindex -- return character indexed by given expression from string

Synopsis
cindex string indexExpr
Description
Returns the character indexed by the expression indexExpr (zero based) from string.

If the expression indexExpr starts with the string ``end'', then ``end'' is replaced with the index of the last character in the string. If the expression starts with ``len'', then ``len'' is replaced with the length of the string.

### verify given strings are same or not
set str1 "wipro"
set str2 "wipro1"
set len1 [llength $str1]
set len2 [llength $str2]
if {$len1 != $len2} {
puts "string are unequal"
} else {
for {set i 0}{$i < $len1} {incr i}{
if{[lindex $str1 $i]==[lindex $str2 $i]} {
set res "equal"
} else {
set res "unequal"
exit
}
}
if {$result == "equal"} {
puts "string are same"
} else {
puts "string are not same"
}
}

### print first and last elements of days
set week {sunday monday tuesday wednesday thursday friday saturday}
foreach a $week {
set b [string range $a 0 0]
set c [string range $a end end]
puts "$b $c"
}

s y
m y
t y
w y
t y
f y
s y

1. Print certain pattern
set x {a##b######c}
regexp {#{4,}} $x a
puts $a
regexp {#{4,}?} $x a
puts $a
regexp {#{1,3}} $x a
puts $a
regexp {#{1,3}?} $x a
puts $a

op
######
####
##
#

2. lappend example
set x {1 2 3}
set y { 4 5 6}
set a [lappend x $y]
puts $a
1 2 3 { 4 5 6}

3. eval example
eval
set str {set lst [list 100 200 300 400]}
set a [eval $str]
puts $a
100 200 300 400

4. foreach example
foreach a [lsort [info commands]] {
puts $a
}

5. info usage
set a [info tclversion]
puts $a
8.3

6.
set a [info commands]
puts $a

7.
set a [info commands l*]
puts $a

8. program to print power of 2
set power 3
set count 0
set res 1
while {$count < $power} {
set res [expr $res*2]
incr count
}
puts "2 to the power $power is $res"
2 to the power 3 is 8

9.
set wait_secs 60
set router [ lindex 1 $argv ]
if [catch "console $router" errmsg] {
error "Unable to connect to the Router\n$errmsg"
}
for {set secs 0;set e0_up 0}{$secs < $wait_secs}{incr secs 6} {
if [regexp {line protocol up} $output] {
set e0_up 1
continue
}
sleep 5
}
if $e0_up {
puts "interface e0 was up within $secs seconds"
} else {
puts "interface e0 did not come up within $secs seconds"
}

10.
set a [info global]
puts $a

argv argv0 tcl_version tcl_interactive errorCode auto_path errorInfo env tcl_patchLevel argc tcl_libPath tcl_platform tcl_library

11.
set var "Some string"
set a [set var]
puts $a
Some string

12. eval usage
set a {puts "this is log file"}
puts $a
eval $a
puts "this is log file"
this is log file

13. compare two strings
set m [string compare "abcd" "abcd"]
puts $m
set n [string compare "abcd" "bcde"]
puts $n
set q [string compare "nawraj" "lekhak"]
puts $q
0
-1
1

14. convert hex to decimal
set hex n
set decimal [string first [string toupper $hex] "0123456789ABCDEF"]
if {$decimal == -1} {
puts "error"
}
puts $decimal
error
-1

or

set hex A
set pattern [split "0123456789ABCDEF" ""]
set dec [lsearch "$pattern" A]
puts $dec

15. string usage
set e [string first a abcd]
puts $e
0

16. convert decimal to hex
set decimal 24
set hex [string index "0123456789ABCDEF" $decimal]
if {$hex == ""} {
puts "$decimal is not in range 0 to 15"
}
puts $hex
24 is not in range 0 to 15

17. string range
set string "abcdefg"
set m [string range $string [expr [string length $string]-3] end]
puts $m
efg

18. convert number to word
set number "2187.003"
set words "zero one two three four five six seven eight nine"
set strlen [string length $number]
for {set i 0} {$i < $strlen} {incr i} {
set char [string index $number $i]
if [ string match {[0-9]} $char] {
puts "[lindex $words $char]"
} elseif {$char == "."} {
puts "point"
}
}
puts {}
two
one
eight
seven
point
zero
zero
three

19. join elements
set a [join {one two three four five } .]
puts $a
set b [split {one/two/three/four/five} /]
puts $b
one.two.three.four.five
one two three four five

20. string match
set a [string match {*love*} "i love tcl"]
puts $a
set a [string match {*[Tt][cc][Ll]} "I love Tcl"]
puts $a
1
1

21.reverse of the list
set mylist {one two three four}
for {set index [expr [llength $mylist]-1]} {$index >= 0} {incr index -1} {
set newlist [lindex $mylist $index]
puts "$newlist"
}
four
three
two
one

22. lreplace example
set a [lreplace {nawraj lekhak raj bhatt} 1 2 hari joshi]
puts $a
nawraj hari joshi bhatt

23. lsearch example
set a [lsearch {one two three four five six} two]
puts $a
set a [lsearch {one two three four five six} fi*]
puts $a
set a [lsearch -exact {one two three four} tw*]
puts $a
1
4
-1

24. info exists usage
if [info exists env(AUTOTEST)] {
puts "everything is ok"
} else {
puts "autotest environment variable is not test"
}

25. switch example
set command "Echo"
switch -glob $command {
{[Ee]*} {
puts "This is an echo command"
}
{[Ii]*} {
puts "info tclversion"
}
default {
puts "invalid command :$command"
}}
This is an echo command

26. regexp usage
set string "%invalid interface E0.\nRouter#"
if {[regexp {^%invalid [Ii]nterface [Ee][0-9]+\.\n[rR]outer} $string c]} {
puts $c
}
%invalid interface E0.
Router

27. factorial proc
proc print {args} {
puts $args
}
print hello world
hello world

proc fact {n} {
if {$n==0||$n==1} {
return 1
} else {
return [expr {$n*[fact [expr {$n-1}]]}]
}
}
set a [fact 4]
puts $a

24. file example
set fd [open file.txt w]
puts "enter the content in the file"
set str [ gets stdin ]
puts $fd $str
close $fd
set fd [open bgp3.tcl.MonJul2006:58:092009.txt w]
puts $fd
set
puts
for {set var 40}{$var < 110} {incr var 10} {
puts [
format \
"Decimal:%3d,Zeropad:%03d,left just:%-3d,\
Hex:%3x,char:%c"\
$var $var $var $var $var ]
}

set list {1 2 3 4 }
set sum 0
foreach var $list {
set sum [expr $sum+$var]
}
puts $sum
10

or
set sum 0
foreach var {1 2 3 4} {
set sum [expr $sum+$var]
}
puts $sum

# while
% while {$i<4} {
> puts "$i*$i is [expr $i*$i]"
> incr i
> }
0*0 is 0
1*1 is 1
2*2 is 4
3*3 is 9

# for
% for {set i 0} {$i<4} {incr i} {
> puts "$i*$i is [expr $i*$i]"
> }
0*0 is 0
1*1 is 1
2*2 is 4
3*3 is 9

Accessing files
Read a specified pattern from a file called HuTclInit containing a string save.

set dir {/usr/local/svi}
set filename HuTclInit
set f [open $dir/$filename r ]
set pattern save
set message "\n"
while {[gets $f line] >= 0} {
if [regexp $pattern $line] {
append message "found: $line \n"
}
}
close $f
set message

op
couldn't open "/usr/local/svi/HuTclInit": no such file or directory

Control flow
In the examples below the scripts are multi line. We advice you to enter these examples in a separate file (see Using scripts from a file on section →[*]).
set message "";
set p 2
foreach k {1 2 3 4} {
append message "\$k to the power \$p is [expr pow(\$k,\$p)]\n"
}
set message

=> 1 to the power 2 is 1.0
2 to the power 2 is 4.0
3 to the power 2 is 9.0
4 to the power 2 is 16.0

#Procedures
proc sum_of_squares {a b} {expr $a*$a + $b*$b}
sum_of_squares 2 5

=> 29

set a 8; set b 9
if {[sum_of_squares $a $b] > 100} {
set message "result is out of limit"
}
set message

=> result is out of limit

#String manipulation
Here is an example on how to extract from a list all the names ending with `.ics'.

set list {a.tiff b.ics c d e.ics d.ics}
set new {}
foreach el $list {
if [string match *.ics $el] {
lappend new $el
}
}
set new

=> b.ics e.ics d.ics

set myArray(foo) "bar"
It is also possible to create an array from a so-called option-value list, for example:
array set fruitColors {tomato red banana yellow}
puts $fruitColors(tomato)
=> red

To process a text file line by line, you have two choices. If the file is smaller than several megabytes, you can read it just in one go:

set f [open $filename]
foreach line [split [read $f] \n] {
# work with $line here ...
}
close $f

For files of any big size, you can read the lines one by one (though this is slightly slower than the above approach):

set f [open $filename]
while {[gets $f line] >= 0} {
# work with $line here ...
}
close $f

file dirname /a/b/foo.html
=> /a/b
file extension /a/b/foo.html
=> .html
file rootname /a/b/foo.html
=> /a/b/foo
file tail /a/b/foo.html
=> foo.html

string first th "The trains were thirty minutes late this past week"
=> 16

string last th "The trains were thirty minutes late this past week"

=> 36

Compare returns 0 if the strings match, -1 if the first string sorts before the second, and 1 if the first string sorts after the second.

string compare twelve thirteen
=> 1

string compare twelve twelve
=> 0

% package require struct 1.3
1.3
% namespace eval my {
namespace import ::struct::list
set l [::list 1 2 3]
puts [list reverse $l]
}
3 2 1

set a {a b c}; set b {d e f}
list $a $b --> {a b c} {d e f}
concat $a $b --> a b c d e f
list {*}$a {*}$b --> a b c d e f

% lindex a
a
% lindex a 0
a
% lindex [lindex a 0] 0
a
% lindex [lindex [lindex a 0] 0] 0
a
% lindex {a}
a
% lindex {a} 0
a
% lindex [lindex {a} 0] 0
a
% lindex [lindex [lindex {a} 0] 0] 0
a
% lindex {{a}}
{a}
% lindex {{a}} 0
a
% lindex [lindex {{a}} 0] 0
a
% lindex [lindex [lindex {{a}} 0] 0] 0
a
% lindex {{{a}}}
{{a}}
% lindex {{{a}}} 0
{a}
% lindex [lindex {{{a}}} 0] 0
a
% lindex [lindex [lindex {{{a}}} 0] 0] 0
a

lvarpop -- delete list element indexed by specified variable

Syntax
lvarpop var [indexExpr] [string]
Description
The lvarpop command pops (deletes) the element indexed by the expression indexExpr from the list contained in the variable var. If index is omitted, then 0 is assumed. If string is specified, then the deleted element is replaced by string. The replaced or deleted element is returned. Thus lvarpop argv 0 returns the first element of argv, setting argv to contain the remainder of the string.

If the expression indexExpr starts with the string ``end'', then ``end'' is replaced with the index of the last element in the list. If the expression starts with ``len'', then ``len'' is replaced with the length of the list.
f {[cequal [info procs Test] {}]} {
source [file join [file dirname [info script]] testlib.tcl]
}

Test list-1.1 {lvarpop tests} {
set a {a b c d e f g h i j}
list [lvarpop a 0] $a
} 0 {a {b c d e f g h i j}}

Test list-1.2 {lvarpop tests} {
set a {a bbbbbb c d e f g h i j}
list [lvarpop a 1] $a
} 0 {bbbbbb {a c d e f g h i j}}

Test list-1.3 {lvarpop tests} {
set a {a bbbbbb c d e f g h i j}
list [lvarpop a 4] $a
} 0 {e {a bbbbbb c d f g h i j}}

Test list-1.3.1 {lvarpop tests} {
set a {a bbbbbb c d e f g h i j}
list [lvarpop a end] $a
} 0 {j {a bbbbbb c d e f g h i}}

Test list-1.3.2 {lvarpop tests} {
set a {a bbbbbb c d e f g h i j}
list [lvarpop a end-1] $a
} 0 {i {a bbbbbb c d e f g h j}}

Test list-1.4 {lvarpop tests} {
set a {a bbbbbb c d e f g h i j}
list [lvarpop a 2 frobozz] $a
} 0 {c {a bbbbbb frobozz d e f g h i j}}

Test list-1.4.1 {lvarpop tests} {
set a {a bbbbbb c d e f g h i j}
list [lvarpop a end frobozz] $a
} 0 {j {a bbbbbb c d e f g h i frobozz}}

Test list-1.4.2 {lvarpop tests} {
set a {a bbbbbb c d e f g h i j}
list [lvarpop a end-1 frobozz] $a
} 0 {i {a bbbbbb c d e f g h frobozz j}}

Test list-1.5 {lvarpop tests} {
set a {a bbbbbb frozbozz d e f g h i j}
list [lvarpop a 2 f] $a
} 0 {frozbozz {a bbbbbb f d e f g h i j}}

Test list-1.6 {lvarpop tests} {
set a {a bbbbbb c d e f g h i j}
list [lvarpop a 20 frobozz] $a
} 0 {{} {a bbbbbb c d e f g h i j}}

Test list-1.7 {lvarpop tests} {
set a {}
list [lvarpop a] $a
} 0 {{} {}}

Test list-1.8 {lvarpop tests} {
set a "a bbbbbb \{a"
lvarpop a 2 frobozz
} 1 {unmatched open brace in list}

Test list-1.9 {lvarpop tests} {
lvarpop
} 1 {wrong # args: lvarpop var ?indexExpr? ?string?}

Test list-1.10 {lvarpop tests} {
unset a
lvarpop a
} 1 {can't read "a": no such variable}

Test list-2.1 {lvarpush tests} {
set a {a b c d e f g h i j}
lvarpush a "xxx"
set a
} 0 {xxx a b c d e f g h i j}

Test list-2.2 {lvarpush tests} {
set a {a c d e f g h i j}
lvarpush a b 1
set a
} 0 {a b c d e f g h i j}

Test list-2.3 {lvarpush tests} {
set a {a bbbbbb c d f g h i j}
lvarpush a e 4
set a
} 0 {a bbbbbb c d e f g h i j}

Test list-2.4 {lvarpush tests} {
set a {a bbbbbb c d e f g h i j}
lvarpush a frobozz 2
set a
} 0 {a bbbbbb frobozz c d e f g h i j}

Test list-2.5 {lvarpush tests} {
set a {a b c d}
lvarpush a e 4
set a
} 0 {a b c d e}

Test list-2.6 {lvarpush tests} {
set a {a b c d}
lvarpush a e 4
set a
} 0 {a b c d e}

Test list-2.7 {lvarpush tests} {
set a {a b c d}
lvarpush a e 14
set a
} 0 {a b c d e}

Test list-2.7.1 {lvarpush tests} {
set a {a b c d}
lvarpush a e end
set a
} 0 {a b c e d}

Test list-2.7.2 {lvarpush tests} {
set a {a b c d}
lvarpush a e end+1
set a
} 0 {a b c d e}

Test list-2.7.3 {lvarpush tests} {
set a {a b c d}
lvarpush a e end-1
set a
} 0 {a b e c d}

Test list-2.8 {lvarpush tests} {
set a "a bbbbbb \{a"
lvarpush a 2 frobozz
} 1 {unmatched open brace in list}

Test list-2.9 {lvarpush tests} {
set a {}
lvarpush a a
set a
} 0 {a}

Test list-2.10 {lvarpush tests} {
unset a
lvarpush a a
set a
} 0 {a}

Test list-2.11 {lvarpush tests} {
set a "a bbbbbb \{a"
lvarpush a 2 frobozz
} 1 {unmatched open brace in list}

Test list-2.12 {lvarpush tests} {
lvarpush
} 1 {wrong # args: lvarpush var string ?indexExpr?}

Test list-3.1 {lvarcat} {
unset a
lvarcat a a b c d e f g
set a
} 0 {a b c d e f g}

Test list-3.2 {lvarcat} {
unset a
lvarcat a a b c d e f g
} 0 {a b c d e f g}

Test list-3.3 {lvarcat} {
unset a
lvarcat a a {b c d} {e f g h}
set a
} 0 {a b c d e f g h}

Test list-3.4 {lvarcat} {
unset a
lvarcat a a {b c d} {e f g h}
} 0 {a b c d e f g h}

Test list-3.5 {lvarcat} {
unset a
lvarcat a a {b {c d}}
lvarcat a {{e f}} g h
set a
} 0 {a b {c d} {e f} g h}

Test list-3.6 {lvarcat} {
unset a
lvarcat a a\{ {b \{c d} \{d
set a
} 0 "a{ b \\{c d {d"

Test list-3.7 {lvarcat} {
lvarcat a
} 1 {wrong # args: lvarcat var string ?string...?}

Test list-3.8 {lvarcat} {
lvarcat
} 1 {wrong # args: lvarcat var string ?string...?}

Test list-4.1 {lcontain} {
lcontain
} 1 {wrong # args: lcontain list element}

Test list-4.2 {lcontain} {
lcontain a b c
} 1 {wrong # args: lcontain list element}

Test list-4.3 {lcontain} {
lcontain {aaaa bbbb cccc} aaaa
} 0 1

Test list-4.4 {lcontain} {
lcontain {aaaa bbbb cccc} a
} 0 0

Test list-4.5 {lcontain} {
lcontain [list a\0\0a bbbb cccc] a\0\0a
} 0 1

Test list-4.6 {lcontain} {
lcontain [list a\0aaa bbbb cccc] a
} 0 0

Test list-4.7 {lcontain} {
# Strings of same length
lcontain {SEEKABLE} SEEKABLE
} 0 1

Test list-5.1 {lempty} {
lempty {}
} 0 1

Test list-5.2 {lempty} {
lempty "\t"
} 0 1

Test list-5.3 {lempty} {
lempty "\tx"
} 0 0

Test list-5.4 {lempty} {
# lempty doesn't check for a valid list.
lempty { {X}xx}
} 0 0

Test list-5.5 {lempty} {
lempty
} 1 {wrong # args: lempty list}

Test list-5.6 {lempty} {
lempty x y
} 1 {wrong # args: lempty list}

Test list-5.7 {lempty} {
lempty "\0"
} 0 0

Test list-5.8 {lempty} {
lempty " \0x"
} 0 0

set a "EeabcDGHF"
if [ string match { [Ee]* } $a ] {
puts "match found"
}else{
puts "match not found"
}
match not found

#Palindrome example
set str1 "wipro"
set len1 [llength $str1]
for {set i 0}{$i <= [ expr $len1/2 ]} {incr i} {
if{[lindex $str1 $i]==[lindex $str2 [ expr $len1-1]]} {
set res "equal"
} else {
set res "unequal"
exit
}
}
if {$result == "equal"} {
puts "string is palindrome"
} else {
puts "string is not a palindrome"
}

set str1 HELLO
set b [string range $str1 1 3]
puts $b
ELL

set str1 HELLO
set b [string match *L? $str1]
puts $b
1

set str1 HELLO
set b [string replace $str1 0 2 naw]
puts $b
nawLO

set str1 qeeeeqqqqHELLOqqqqqqqq
set b [string trim $str1 q]
puts $b
HELLOqqqqqqqq

set list1 {1q 28 3n 4o}
set b [lindex $list1 2]
puts $b
3n

set var "some string"
set var2 [set var]
puts $var2
some string

set var {zero one two}
set b [ lsearch {zero one two} two ]
puts $b
2
set var {zero one two}
set b [ join {zero one two} : ]
puts $b
zero:one:two

if [ string match {[Ee]*} $var ] {
puts "match found"
} else {
puts "match not found"
}

error
if [catch {set fd [open raj.txt]} error] {
error "$error"
}
while{[gets $fd string ]!= -1}{
puts $string
}
if ![ eof $fd ]{
error "error occured" }
}
close $fd

couldn't open "raj.txt": no such file or directory

set b {1 2 3 4 }
foreach var $b {
if {$var%2==0} {
puts "$var"
}
}
2
4

set x {dolores Sanchez sanjose CA}
set b [ regexp {^dolores} $x match ]
puts $b
1

regexp uasge:
crashinfo_200\b[0-9]{5}\b\-[0-9]{6}\.[0-9]{2}
regexp "crashinfo_((200[6-8])([0-1]|[1-2])([0-2][1-9]|3[0-2])-[0-9].*)" $sample match
puts "$match"
regexp -line "crashinfo.*" $sample match
or
set s {1 .. image FFC223A2 39F864 27 3536868 Dec 12 2006 02:34:01 +00:00 c12kprp-boot-mz.120-31.3.S1
2 .. unknown 00000001 39F8E4 28 0 Dec 13 2006 01:49:12 +00:00 crashinfo_20061213-014912.10
3 .. unknown 00000001 39F964 28 0 Dec 13 2006 23:23:46 +00:00 crashinfo_20061213-232346.10
4 .. unknown 00000001 39F9E4 28 0 Dec 28 2006 21:39:20 +00:00 crashinfo_20061228-213920.10
5 .. unknown 00000001 39FA64 28 0 Jan 2 2007 21:33:16 +00:00 crashinfo_20070102-213316.10
6 .. unknown 00000001 39FAE4 28 0 Jun 14 2007 18:32:23 +00:00 crashinfo_20070614-183223.10
7 .. unknown C506747F 3D01D8 27 198258 Jun 15 2007 11:45:04 +00:00 crashinfo_20070615-114504.9
8 .. unknown 9363DF7F 403834 27 210393 Jun 17 2007 10:24:30 +00:00 crashinfo_20070617-102430.9
9 .. unknown 42DC57DC 43E650 27 241052 Jun 17 2007 10:27:41 +00:00 crashinfo_20070617-102741.9
10 .. unknown 39EFBCE0 46F09C 27 199115 Jun 17 2007 10:31:27 +00:00 crashinfo_20070617-103126.9
11 .. unknown 6FCAA563 4A9D8C 27 240750 Jun 17 2007 10:38:53 +00:00 crashinfo_20070617-103853.9
12 .. unknown 4E4134DA 4DA794 27 199047 Jun 27 2007 11:35:19 +00:00 crashinfo_20070627-113519.9
13 .. unknown 382A5ED0 52EBD8 25 345026 Jul 28 2008 21:07:56 +00:00 crashinfo_20080728-210756
14 .. unknown 66F6E8A6 580FC4 25 336747 Jul 28 2008 21:09:15 +00:00 crashinfo_20080728-210915
15 .. unknown 154028B7 585E88 28 20036 Nov 4 2008 04:47:50 +00:00 crashinfo_20081104-044750.10
16 .. unknown ED1AB502 58AD74 28 20076 Nov 4 2008 21:07:41 +00:00 crashinfo_20081104-210741.10

60773004 bytes available (5549428 bytes used)
}
foreach a $s {
puts $a
if {[regexp {crashinfo_[0-9]*-[0-9]*.[0-9]*} $a aa]} {
puts "$aa"
}
}

4) regexp example
set x http://www.activestate.com
regexp {(?:http|ftp)://(.*)} $x aa
puts $aa
http://www.activestate.com

5)
set a {SLOT 10 (RP/LC 10): Modular SPA Interface Card (10G)
MAIN: type 149, 800-26270-01 rev 96
Deviation: 0
HW config: 0x00 SW key: 00-00-00
PCA: 73-9607-02 rev 75 ver 2
Design Release 1.0 S/N SAD09060AT7
MBUS: Embedded Agent
Test hist: 0x00 RMA#: 00-00-00 RMA hist: 0x00
DIAG: Test count: 0x00000000 Test results: 0x00000000
FRU: Linecard/Module: 12000-SIP-601=
Processor Memory: MEM-LC5-2048=(Non-Replaceable)
Packet Memory: MEM-LC5-PKT-512=(Non-Replaceable)
L3 Engine: 5 - ISE 10 Gbps
MBUS Agent Software version 2.68 (RAM) (ROM version is 3.23)
ROM Monitor version 255.255
Fabric Downloader version used 4.1 (ROM version is 255.255)
Primary clock is CSC 1
Board is analyzed
Board State is Line Card Enabled (IOS RUN )
Insertion time: 00:00:08 (02:12:14 ago)
Processor Memory size: 2147483648 bytes
TX Packet Memory size: 268435456 bytes, Packet Memory pagesize: 32768 bytes
RX Packet Memory size: 268435456 bytes, Packet Memory pagesize: 32768 bytes
0 crashes since restart

SPA Information:
subslot 10/0: SPA-4XT3/E3 (0x40B), status is ok
subslot 10/1: Empty
subslot 10/2: SPA-8XFE-TX (0x4C5), status is ok
subslot 10/3: SPA-1XCHSTM1/OC3 (0x463), status is ok
High Speed SPA allowed in subslot 10/2 and 10/3: No
}
set a [split $a "\n"]
foreach s $a {
if {[regexp SPA-4XT3/E3 $s] & [regexp {status is ok} $s]} {
puts "SPA-4XT3/E3 is up"
}
}
SPA-4XT3/E3 is up

7
set a bat
if {[regexp {b.t} $a raj ]} {
puts "$raj"
}

8
set str "this is my car"
regsub {\s is} $str was aa
puts $aa
thiswas my car

9
set str "LCNO-04-08-KAR-001"
if {[regexp {^LCNO-[0-9]*-[0-9]*-[A-Z]*-[0-9]*} $str naw]} {
puts "$naw"
}
LCNO-04-08-KAR-001

10
set str "hello world this is the simple text"
if {[regexp {text$} $str]} {
puts "match found "
} else {
puts "match not found"
}
match found

11.
set date "31/12/2009"
if {[regexp {^[0-9]*/[0-9]*/[0-9]*} $date a]} {
puts $a
}
31/12/2009

12
set string "one: 1, two: 2, three: 3"
if {[regexp {^[a-z]*: [0-9], [a-z]*: [0-9], [a-z]*: [0-9]} $string aa]} {
puts $aa
}
one: 1, two: 2, three: 3
#for checking purpose
set b [ regexp {^[a-z]*: [0-9], [a-z]*: [0-9], [a-z]*: [0-9]} $string ]
puts $b
regexp {[tT]wo: ([0-9]+)} $string match submatch
puts $match
puts $submatch

13.
set s "c12kprp-boot-mz.120-31.3.S1"
if {[regexp {c[0-9]*[a-z]*-[a-z]*-[a-z]*.[0-9]*-[0-9]*.[0-9].[A-Z][0-9]} $s bb]} {
puts $bb
}

set s {1 .. image FFC223A2 39F864 27 3536868 Dec 12 2006 02:34:01 +00:00 c12kprp-boot-mz.120-31.3.S1
2 .. unknown 00000001 39F8E4 28 0 Dec 13 2006 01:49:12 +00:00 crashinfo_20061213-014912.10
3 .. unknown 00000001 39F964 28 0 Dec 13 2006 23:23:46 +00:00 crashinfo_20061213-232346.10
4 .. unknown 00000001 39F9E4 28 0 Dec 28 2006 21:39:20 +00:00 crashinfo_20061228-213920.10
5 .. unknown 00000001 39FA64 28 0 Jan 2 2007 21:33:16 +00:00 crashinfo_20070102-213316.10
6 .. unknown 00000001 39FAE4 28 0 Jun 14 2007 18:32:23 +00:00 crashinfo_20070614-183223.10
7 .. unknown C506747F 3D01D8 27 198258 Jun 15 2007 11:45:04 +00:00 crashinfo_20070615-114504.9
8 .. unknown 9363DF7F 403834 27 210393 Jun 17 2007 10:24:30 +00:00 crashinfo_20070617-102430.9
9 .. unknown 42DC57DC 43E650 27 241052 Jun 17 2007 10:27:41 +00:00 crashinfo_20070617-102741.9
10 .. unknown 39EFBCE0 46F09C 27 199115 Jun 17 2007 10:31:27 +00:00 crashinfo_20070617-103126.9
11 .. unknown 6FCAA563 4A9D8C 27 240750 Jun 17 2007 10:38:53 +00:00 crashinfo_20070617-103853.9
12 .. unknown 4E4134DA 4DA794 27 199047 Jun 27 2007 11:35:19 +00:00 crashinfo_20070627-113519.9
13 .. unknown 382A5ED0 52EBD8 25 345026 Jul 28 2008 21:07:56 +00:00 crashinfo_20080728-210756
14 .. unknown 66F6E8A6 580FC4 25 336747 Jul 28 2008 21:09:15 +00:00 crashinfo_20080728-210915
15 .. unknown 154028B7 585E88 28 20036 Nov 4 2008 04:47:50 +00:00 crashinfo_20081104-044750.10
16 .. unknown ED1AB502 58AD74 28 20076 Nov 4 2008 21:07:41 +00:00 crashinfo_20081104-210741.10

60773004 bytes available (5549428 bytes used)
}
if {[regexp {c[0-9]*[a-z]*-[a-z]*-[a-z]*.[0-9]*-[0-9]*.[0-9].[A-Z][0-9]} $s bb]} {
puts $bb
}

14.
regexp {c((.*)g)(.*)} "abcdefghi" matched sub1 sub2 sub3
puts $matched
puts $sub1
puts $sub2
puts $sub3
op
cdefghi
defg
def
hi

15.
set str 66.70.7.154
regexp "(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})" $str all first second third fourth
puts "$all \n $first \n $second \n $third \n $fourth \n"
op
66.70.7.154
66
70
7
154

16.
set str {Acc No: 12345}
set num [regexp {.*?(\d+).*} $str junk result]
puts $result
1
set num [regexp {(\d+)} $str junk result]
puts $result
12345

17.
set word "foo"
set result [regexp {(foo|bar)} match zzz]
set zzz
can't read "zzz": no such variable
???

18.
set word "foo"
set result [regexp {(foo|bar)} $word match zzz]
puts $match
puts $zzz
puts $result
op
foo
foo
1

19.
set str " sjkhf sdhj "
set rest [regexp {^ +.* +$} $str match]
puts $rest
op
1

20.
{^[A-Za-z]+$} Only letters.
{^[[:alpha:]]+$} Only letters, the Unicode way.

21.
set y {123zzz456}
regexp {3z*} $y match; set match
3zzz
regexp {3z*?} $y match; set match
3

22.
% set x {a##b#######c}
a##b#######c
% regexp {#{4,}} $x match; set match
#######
% regexp {#{4,}?} $x match; set match
####

23.
% regexp {[[.number-sign.]]+} {123###456} match
1
% set match
###

24.
% set input "cchchh"
cchchh
% regexp {[[.ch.][.c.]]+} $input match; set match
cchch

25.
If you don't want parentheses to capture text, add ?: after the opening parenthesis. For instance, in the example below, the subexpression (?:http|ftp) matches either http or ftp but doesn't capture it. So the back reference \1 will hold the end of the URL (from the second set of parentheses):


% set x http://www.activestate.com
http://www.activestate.com
% regsub {(?:http|ftp)://(.*)} $x {The hostname is \1} answer
1
% set answer
The hostname is www.activestate.com

26.
Lookahead Assertions
There are times you'd like to be able to test for a pattern without including that text in the match. For instance, you might want to match the protocol in a URL (like http or ftp), but only if that URL ends with .com. Or maybe you want to match the protocol only if the URL does not end with .edu. In cases like those, you'd like to "look ahead" and see how the URL ends. A lookahead assertion is handy here.

A positive lookahead has the form (?=re). It matches at any place ahead where there's a substring like re. A negative lookahead has the form (?!re). It matches at any point where the regular expression re does not match. Let's see some examples:


% set x http://www.activestate.com
http://www.activestate.com
% regexp {^[^:]+(?=.*\.com$)} $x match
1
% set match
http
% regexp {^[^:]+(?=.*\.edu$)} $x match
0
% regexp {^[^:]*(?!.*\.edu$)} $x match
1
% set match
http

The regular expressions above may seem complicated, but they're really not bad! Find the lookahead expression in the first regexp command above; it starts with (?= and ends at the corresponding parenthesis. The "guts" of this lookahead expression is .*\.com$, which stands for "a string that ends with .com". So the first regexp command above matches any string containing non-colon (:) characters, as long as the rest of the string ends with .com. The second regexp is similar but looks for a string ending with .edu. Because regexp returns 0, you can see that this doesn't match. The third regexp looks for a string not ending with .edu. It matches because $x ends with .com.

27.
% set x http://www.activestate.com
http://www.activestate.com
% regexp {^[^:]+(?=.*\.com$)} $x match
1
% set match
http
% regexp -expanded {
^ # beginning of string
[^:]+ # all characters to the first colon
(?= # begin positive lookahead
.*\.com$ # for a trailing .com
) # end positive lookahead
} $x match
1
% set match
http

28.
With the -line switch, the metacharacters ^, $, ., and [] treat a newline as the end of a "line." So, for example, the regular expression ^San Jose matches the second line of input below:
% set x {Dolores Sanchez
San Jose, CA}
Dolores Sanchez
San Jose, CA
% regexp {^San Jose} $x match
0
% regexp -line {^San Jose} $x match
1
% set match
San Jose

29.
This table below summarizes the new syntax:
{m} Matches m instances of the previous pattern item
{m}? Matches m instances of the previous pattern item. Non-greedy.
{m,} Matches m or more instances of the previous pattern item.
{m,}? Matches m or more instances of the previous pattern item. Non-greedy.
{m,n} Matches m through n instances of the previous pattern item.
{m,n}? Matches m through n instances of the previous pattern item. Non-greedy.
*? Matches zero or more of the previous pattern item. Non-greedy.
+? Matches one or more of the previous pattern item. Non-greedy.
?? Matches zero or one of the previous pattern item. Non-greedy.
(?:re) Groups a subpattern, re, but does not capture the result.
(?=re) Positive lookahead. Matches the point where re begins.
(?!re) Negative lookahead. Matches any point where re does not begin.
\c One of many backslash escapes.
[. .] Delimits a collating element within a bracketed expression.
[= =] Delimits an equivalence class within a bracketed expression.
[: :] Delimits a character class within a bracketed expression.
(?abc) Embedded options a, b, and c
*** Director

Some of the new switches for regexp and regsub are:
-expanded Enable expanded syntax (for comments)
-line Enable newline-sensitive matching
-linestop Make [] and . stop at newlines.
-lineanchor Make ^ and $ match the start and end of a line.

30.
regsub {(..)(...)} ab123 {\2\1=\0}
123ab=ab123

31.
regsub -all , 1,234,567.89 ""
1234567.89

string map {, ""} 1,234,567.89
1234567.89

regexp {(?x) # A pattern to match URLS
([^:]+): # The protocol before the initial colon
//([^:/]+) # The server name
(:([0-9]+))? # The optional port number
(/.*) # The trailing pathname
} $input

set a 10;set b 20
set a [expr $a +$b]
puts $a

set a 10;set b 20
set c [expr { $a+$b }]
puts "$c" or $c

1. TCL provides commands for dealing with process and allows the creation of new process through “
Env
Exec ans
Both
None

2. lsearch uses:
glob ans
regex
regsub
all

3. Main Widget occupies:
Mid window
Main window
Top level
Down level

4. to execute a subprocess exec looks for an executable file with name equals to exec

5. Syntax of regexp
Regexp
Pattern
String
All ans

6. with the –level ) option return can serve as a replacement for
continue
break ans
return
none

7. env get returns a value of specified environment
collection
arrays
variables ans
none

8. The uplevel command causes the invoking procedure to disappear from the
procedure calling stack while the command is being
called and linked to create a different stack
Stack frame return a null string
Executed and called into a different level of stack ans

9. In puts command fileid defaults to
stdin
stdout ans
both
all

10. if file is followed with tail and name of the file, it returns the last value of name called:
end of file
pathname ans
name and value
none

11.Each geometry manager implements a different style of
Packaging
Communication with different widgets
Different layouts
Layout

11. A while command takes how many arguments
1
3
2 ans
None

12. if file with fileid followed with type and name it return a string giving the type of:
filename ans
file
directory
link all

13. 2 ways of input focus
Implicit
Explicit
Both asn
None

14. Tell command return a value at eof on fileid
Positive
Eof
Bof
True ans

15. Script evaluated using uplevel in variable context of different stack level like
Upvar ans
uplevel
eval
none

16. while repeatedly tests
Boolean ans
Array
Procedure
Substitution

17. 4 main grounds of tcl commands

18. in the cavity model the widget placed inside occupies one whole side of
Frames
Canvas
Anchor
Cavity

19. A TCL procedure is a command implemented with a TCL script rather than
Java
C ans
C++
All

20. Bitmaps in TK supported with
frames
labels
pack
all

21. TK applications are controlled by 2 kinds of TCL scripts
Initiate
Event handler
API
Both a and b

1) Tk was designed for which language?
a. ruby
b. Perl
c. python
d. All -- answer.

2) Exit statement in tcl will return what value if not specified any thing?
a. Zero -- answer.
b. -1
c. 1
d. None

(Conventionally, an exit status of 0 indicates a successful termination, while any other number indicates failure. The most common value used to signal failure is 1, but some programs use a range of small numbers to indicate why or how they failed.)

3) If we unset the upvar then what will happen
a. upvar variable is unset
b. variable to which it is referencing is unset - answer
c. …
d. None

Note: the unset operation affects the variable it is linked to, not the upvar variable. There is no way to unset an upvar variable except by exiting the procedure in which it is defined. However, it is possible to retarget an upvar variable by executing another upvar command.

4) Packages in Tcl /Tk are
a. Modules
b. Reusable objects
c. Code – answer. (http://www.wellho.net/mouth/146_example-of-Tcl-namespaces-and-packages.html)
d. None

5) Tcl procedures are scripts written in tcl commands are in
a. java code
b. c -- answer
c. c++
d. none

6) If Tcl is upgraded with some files….it is effects …
a. Compiler
b. Interpreter – answer (Tcl is an interpreted language)
c. ..
d. None

7) What is ENV (global array env contains an entry for each environment variable)
a. Function
b. Variable -- ans
c. Command
d. None

8) Some questions on glob, [glob {^+.*$} $str match]…some thing like this.

9) Some questions on lsearch

10) Some upvar program.
Upvar 1 $y z
Upvar 2 x a
a. upvar set x to 2 levels up the a
b. Tie, x to 2 levels up the a – answer.
c. ..
d. None

11) Programs on proc

12) Variable defined outside the procedure has
a. local scope
b. global scope -- answer
c. ..
d. None

13) Read command reads block of
a. Data – answer (block of data and not block of bytes)
b. Bytes
c. Bytes and data
d. None

14) Tcl variables can be manipulated as strings even
a. script
b. code ans
c. collections
d. none

15) Some programs on files.

16) Some programs on regexp.

17) Some program on “foreach”

18) Moving objects in tk can be achieved in
a. Canvas -- answer
b. widgets
c. window
d. none

19) invoke can be used for widgets created and modified by
a. configure command
b. pack
c. …
d. Some other option

20) Is the GUI programs(tk) written in
a. procedures
b. modules
c. …
d. …

It was really tough …. Need to get more questions.
1. tk designed for
a. ruby,
b. perl
c. python
d. none -- ans

2. exit takes
a. string
b. interger -- ans
c. collection
d. none

3. to set the window to default size
a. wm geometry -- ans
b. wm geometry .w { }
c. wm.w geometry .w[]
d. none

4. global scope is of what level
a. global scope
b. top level -- ans
c. uplevel
d. upvar and uplevel

5. lsearch {a b c d } c
lsearch {a b c b g c} c
a. 2
b. 2 5
c. Both a and b -- ans
d. None

6. how can be the procedure deleted from inside a script
a. rename procedure “” -- ans
b. ….
c. None

7. frame in tk
a. widget
b. window
c. …
d. Do not respond to event handler. -- ans

8. info level \’\’ will give
a. level of the called procedure
b. level of calling procedure -- ans
c. ..
d. None

9. read command has 2 forms like
a. read fileid without newline character
b. read fileid with number of bytes
c. both a and b -- ans
d. none

10. if in file operation no file extension is specified then
a. will return fileid
b. will return empty string ans
c. will return fileid with directory
d. none

11. env is
a. collectin
b. function
c. procedure
d. array  ans

12. for text related widgets …
a. text -- ans
b. canvas
c. ..
d. None

13. widget is positioned at centre of the window by
a. anchor --- ans (but I think it is default)
b. canvas
c. default
d. none

14. upvar and uplevel --- read

15. string match question

16. regexp.

1)All data type can be manipulated in to strings
a) TCL scripts,
b) TCL module
c) TCL procedure
d) All of the above

2)Associated list can be formed by
a) Array ans
b) List
c) Associated list
d) Some key options

3) regsub take how many arguments
a) two
b)three
c)Four ans
d)Five

4)list x{ 1 2 3 4 5} expr{$x*$x}
a) { 1 4 9 16 25 }
b) { 1 4 9 16 25 }
c) Both a and b
d) { 1 4 9 16 26 }

5)File creates for read and write if not exist and deletes all the contents.
a) r
b) r+
c) w+ ans
d) a=

6) question on grid

7) TK window is displayed by default
a) focused size
b) natural size
c) both
d none

8) tk line can be placed in
a) row
b)Column
c)Both
d)All of the above
9 Geometry manager
a)grid
b)place
c) –----
d) all of the above


1. if {$argc} {
set file [lindex $argv 0]
}else{
tk_messageBox - message
Usage \n\"
exit 0
}
}

2. Tk also provide geometry manager
a. place
b.grid
c.pack
d.all

3.look and feel of widget can be hardcoded in
a. tcl
b.tk
c.both
d.all

4.listbox is a widget that display list of string one per line when first created a listbox has
a. proc
b.array
c.modes
d.all

5.By default each top level window appears
a. focus size
b.natural size
c.both
d. all

6.source command will read file upto
a. eof
b.Ascii value “\32” ans
c.upto last char
d.all of the above

7.most of the tcl script contain keyword
a.arguments ans
b parameters
c.procedures
d.functions
8. When open command with process id is used in TCL return value of the process id is
a.file id ans
b.sub process
c.arg and exec
d.implicit process

9. hello proc apple {} {
Puts “hi”
Puts $apple }
a. hello
b. set apple
c. apple
d. error ans

10.command regexp1 | regexp 2 matches
a.regexp1
b. regexp2
c.regexp
d.all ans is either regexp1 or regexp2

11.file with join command takes the arguments as a
a.dir name
b.filename.
c.pathname
d.newpathname ans

12.close command frees operating system related to
a.input
b. output
c. input and output ans
d.all

13. calling of unknown handlers registered with
a.namespace
b.unknown
c.namespace and unknown ans
d.none

14.in switch if it matches two option first option is
a.checked
b.evaluated ans
c.validated
d.all
15. if {[regexp (c(.*)g(.*)) cdefghi matches subvar1 subvar2 subvar3]} {
puts $matches
puts $subvar1
puts $subvar2
puts $subvar3
}
a. cdefghi
b. defghi
c. def and hi
d. all of the above

output is
cdefghi
cdefghi
def
hi

1) TCL_GET version gets the version of
a) packages
b) libraries ans
c) binary files
d) none.

2) ITCL is an object system for
a) unix
b) shell
c) object oriented for TCL
d) all
Itcl is an object system for Tcl,
3) arranging the element with a layout is called
a)widgets
b)geometry manager
c) configration options
d)bindinds

4)The file with join command takes a argument as a
a)directory name
b)file name
c)path name
d)new path name

5)The command regexp1|regexp2 matches
a)regexp1
b)regexp2
c)regexp1 and regexp2
d)all

6)IN Puts command the feild defalts to
a)stdin
b)stdout ans
c)stdin and stdout
d)all

7)The upvar comand simplifys the implimentation of
a)pass by refference
b)call by name procedure ans
c)reeference proc
d)all the above

8)The output of
proc example {first {second ""}args} {
if {$second eq ""} {
puts " This is only one argument and it is ;$first"
return 1
a)absolute error
b)eq operator cannot be used with arguments
c)code is incomplete
d)result is 1

9)by defalt there is a single global scope for
a) procedure variable
b) array variable
c) list variable
d) proc name ans

10) The close command frees the operating system resources assosiated with
a) input
b)output
c) input & output ans
d)all

11) when you try to divide code in TCL it is called
a) module
b)packages
c)libraries
d)none

12) The source command will read files up to the
a) end of file
b) ASC 32 char ans
c) last charector
d) all

13)exec supportes redirection in the fashion similar to
a)windows
b)DOS
c)unix ans
d)all

14)Gridding in canvas supports for
a)scrolling
b)gridding
c)pointer position
d)post script

15)ENV variables can be read and written using the standard TCL
a)command variables
b)variable mechanism ans
c)pid command
d)none
Environment variables can be read and written using the standard Tcl variable ...
16) Join string argument defalts to a
a)empty string
b)defalt string
c)space character ans
d)all of above

17)Env get returns the value of the specified enviorment
a)collections
b)array
c)variables ans
d) none
Returns the value of the specified environment variable
18) THe output of
set b [list a b {c d e}{f{g h}}]
puts "treated as a list : $b /n"
a) code incompleate
b)treated as a list :a b {c d}{f {gh}}
c)none ans
d)set with list gives an error.

19)The first argument to the array command has various info like
a)size
b)elements
c)etc
d)all ans

20)A single handler proc could be used for several namespaces with the namespace passed as an
a)path
b)unknown
c)arguments ans
d)all

a single handler proc could be used for several namespaces with the namespace passed as an argument: ...

21)The GUI of program is commonly constructed
a)global
b)local
c)proc
d)cascading manner

22)an application that requires this charecters in file may self encode it as
a) /032
b)may generate it by use of command such as format in binary
c)command file
d)both a & b ans

23)If level is 2 the code is execute on the context of the caller of the
a)caller procedure
b)function procedure.

if level is 2 the code is executed in ... that will execute the script "puts $myvar" in the context of the caller of the caller ...

24)look and feel of widget can be hardcoded in
a) tcl
b) tk
c) both
d) all

25)Tk also provide geometry manager

a) place
b) grid
c) pack
d) all




2 comments:

  1. thank you sir, this will help me build my skills in TCL

    ReplyDelete
  2. I am glad you found it useful !!Happy learning !!

    ReplyDelete