Monday, 27 February 2017

TCL INTERVIEW QUESTIONS -Part2

1. 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

Using regsub it can be swapped in two lines or one line also as below  

ALITER
set ip 192.30.40.1
regsub {(\d\d).(\d\d).(\d\d).(\d\d)} $ip {\1.\3.\2.\4} ip1
puts $ip1

ALITER
set ip 192.30.40.1
regsub {(\d+).(\d+).(\d+).(\d+)} $ip {\1.\3.\2.\4} ip1
puts $ip1

ALITER

set a 192.30.40.1
set b [ split $a .]
set u [lindex $b 0]
set v [lindex $b 3]
set x [lindex $b 1]
set y [lindex $b 2]
set z [join "$u $y $x $v" .]
puts $z

ALITER
set ip 192.30.40.1
regexp {([0-9]+\.)([0-9]+\.)([0-9]+\.)([0-9]+)} $ip match 1st 2nd 3rd 4th
append new_ip $1st $3rd $2nd $4th
puts $new_ip


2. 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

3.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”
o/p:
The given string malayalam is palindrome
The given string welcome is not palindrome

ALITER
set a madam
set len [string length $a]
set n [expr ($len-1)/2]
for {set i 0} {$i < $n} {incr i} {
set b [string index $a $i]
set c [expr $len - 1 - $i]
set d [string index $a $c]
if {$b != $d} {
puts "not palindrome"
exit
}
}
puts "palindrome"

4.How increment a character? For example, I give 'a' and I
should get 'b'.

set ch a
set l [scan $ch %c]
incr l
set m [format %c $l]
puts "$m"


5.{Anu Anudeep Anukumar Amar Amaravathi Aruna}
is their any possibility to find the letter "a"in the given list? if yes how?

puts "searching variable a"
set list_4 [list Anu Anudeep Anukumar Amar Amaravathi Aruna]
foreach item $list_4 {
set elem [split $item ""]
foreach var $elem {
if {$var == "a"} {
puts $item
}
}
}

ALITER

set str {Anu Anudeep Anukumar Amar Amaravathi Aruna}
foreach i $str {
if {[regexp {(A)} $i match ] } {
puts $match
}
}

6.Create a list of week days and print the first and last
character of each day using foreach command.
 % set days {monday tuesday wednasday thursday friday
saturday sunday}
monday tuesday wednasday thursday friday saturday sunday
% foreach day $days {
set str [split $day ""]
puts "[lindex $str 0]"
puts "[lindex $str end]"
}

7. Write a proc to increment the ip by the given no. of times.The incremented IPs should be a valid one.
Ex: proc <name> {ip no_of_incrments} {
body
}
proc increment_ip {ip no_of_inc} {
puts "Ip address --- $ip"
for {set inc 1} {$inc<=$no_of_inc} {incr inc} {
set ip_list [split $ip "."]
set oct1 [lindex $ip_list 0]
set oct2 [lindex $ip_list 1]
set oct3 [lindex $ip_list 2]
set oct4 [lindex $ip_list 3]
incr oct4
if {$oct4>255} {
set oct4 0
incr oct3
}
if {$oct3>255} {
set oct3 0
incr oct2
}
if {$oct2>255} {
set oct2 0
incr oct1
}
if {$oct1>255} {
incr oct1 -1
puts "cannot increment ip"
exit
}
set ip $oct1.$oct2.$oct3.$oct4
puts "next ip -- $ip"
}
}

increment_ip 1.1.1.1 10


8. How to get the next ip for given ip
ex: 10.10.10.1 -> 10.10.10.2
ex: 10.10.10.255 -> 10.10.11.0

set ip "10.10.10.255"
set list [split $ip "."]
set ww [lindex $list 0]
set xx [lindex $list 1]
set yy [lindex $list 2]
set zz [lindex $list 3]

if {$zz>254} {
set zz 0
incr yy
if {$yy>255} {
set yy 0
incr xx
if {$xx>255} {
set xx 0
incr ww }
if {$ww>255} {
set ww 0
}}} else {
incr zz
}
puts "$ww.$xx.$yy.$zz"

Friday, 24 February 2017

TCL PACKAGES

Packages:
•       Tcl provides a more powerful mechanism for handling reusable units of code called packages
•       A package is simply a bundle of files implementing some functionality, along with a name that   
         identifies the package, and a version number that allows multiple versions of the same
         package to
         be present 
•       A package can be a collection of Tcl scripts, or a binary library, or a combination of both 

How to Create Package:
•       Adding a package provide statement to your script 
•       Creating a pkgIndex.tcl file. 
•       Installing the package where it can be found by Tcl. 
•       Commands
•       package require ?-exact? name ?version?
•       package provide name ?version?

Sample Packages:
Example:
package require TclUtils


1. Use of package command?
Package is similar to libraries except that they require explicit loading.

2..Use of package require command?
Package require command organizes sets of procedures under a single name, and lets you request packages by name and revision number.

3. What are the possible package require problem?
Say package A and package B both define procedure name sum. If these two packages are loaded and each contains same proc name, then the last package loaded will define the procedure, even it is different from the first procedure.


Solution: namespaces will help alleviate this problem.


How to Create a package in TCL see the below link.

TCL PROC

Procedures are nothing but code blocks with series of commands that provide a specific reusable functionality. It is used to avoid same code being repeated in multiple locations. 
Procedures are equivalent to the functions used in many programming languages and are made available in Tcl with the help of proc command.
The syntax of creating a simple procedure is shown below −

proc procedureName {arguments} {
   body
}

 
      Procedure provides following benefits
         Easy maintenance – changes needs to be made only at one place
         Saves space, time and effort as a function is implemented only once
         Procedures can be shared when placed in “libraries”

           
A simple example for procedure is given below –

proc helloWorld {} {
   puts "Hello, World!"
}
helloWorld

When the above code is executed, it produces the following result −
Hello, World!


Procedures with Multiple Arguments
An example for procedure with arguments is shown below –

proc add {a b} {
   return [expr $a+$b]
}
puts [add 10 30]

When the above code is executed, it produces the following result –
40

Procedures with Variable Arguments
An example for procedure with arguments is shown below –

proc avg {numbers} {
   set sum 0
   foreach number $numbers {
      set sum  [expr $sum + $number]
   }
   set average [expr $sum/[llength $numbers]]
   return $average
}
puts [avg {70 80 50 60}]
puts [avg {70 80 50 }]

When the above code is executed, it produces the following result –
65
66

Procedures with Default Arguments
Default arguments are used to provide default values that can be used if no value is provided. An example for procedure with default arguments, which is sometimes referred as implicit arguments is shown below –

proc add {a {b 100} } {
   return [expr $a+$b]
}
puts [add 10 30]
puts [add 10]

When the above code is executed, it produces the following result −
40
110


Recursive Procedures
An example for recursive procedures is shown below –

proc factorial {number} {
   if {$number <= 1} {
      return 1
   }
   return [expr $number * [factorial [expr $number - 1]]]

}
puts [factorial 3]
puts [factorial 5]


When the above code is executed, it produces the following result −
6
120


4 ways to pass an arguments to procedure.

Pass by value
Proc sum {a b} {
Set num [expr $a + $b]
Puts “The sum is: $num”
}
Sum 2 3

Pass by name
Array set months {1 Jan 2 Feb}
Parray months                                                                                                                                
Or
Proc increase {initial_apy change} {
Upvar $initial_pay x
Foreach item [array names x] {
Set x($item) [expr $x($item) + $change]
}
}
Array set Pay {Ray Steve 50 Fred 200}
Increase Pay 25
Parray Pay

Defaults
Proc myprocdefault  {{}{}{}} {
Puts “$a $b $c”
}
Myprocdefault


Variable arguments
Proc show {a args} {
Puts $a
foreach val  $args {puts $val}
}
Show 1 2 3


Create a procedure that will accept one argument and variable arguments

proc variable {a args} {
puts $a
foreach val $args { puts $val }
}
variable 1 2 3 4 5

Output:
1
2
3
4
5

Example Variable number of arguments. 

proc args_parser {args} {
    set length [llength $args]
    puts "Length of the args is $length"
    for { set i 0 } { $i < $length } { incr i } {
        set arg_name [lindex $args $i]
        incr i
        set arguments($arg_name) [lindex $args $i]
    }
    return [array get arguments]
}

proc b {args} {
    array set ar [eval args_parser $args]
    set name $ar(-name)
    set age $ar(-age)
    set country $ar(-country)
    set location $ar(-location)
    puts "$name is aged $age\n";
    puts "$name is located at $location,$country\n";
}

b -name manish -age 27 -location bangalore -country India