Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

TCL Notes9

Uploaded by

SANGAMESH V A
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

TCL Notes9

Uploaded by

SANGAMESH V A
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit : TCL SES9

Procedures can be used to simplify code and make it more modular and reusable. They
are an important part of the TCL language and are used extensively in TCL
programming.

what is the uses of the proc in tcl ?

1. Modular programming: Procedures allow you to break up your code into smaller,
more manageable pieces. This makes your code easier to read, understand, and
maintain.

2. Code reusability: Once you define a procedure, you can call it multiple times
from different parts of your code, without having to write the same code again and
again. This saves time and reduces the risk of errors.

3. Parameterization: Procedures can take input parameters, which allows you to pass
in different values and customize the behavior of the procedure based on those
values. This makes your code more flexible and adaptable.

4. Encapsulation: Procedures can be used to encapsulate complex logic or


algorithms, making it easier to reuse or share that logic with other developers.

write a procedure without the argments:

proc show {} {
puts "hello world!"
}

show

write proc with return

proc show {} {
set a "welcome to bangalore"
return $a
}

puts [show]

write a proc with variable number of the arguments ?

proc sum {args} {


set s 0
foreach arg $args {
incr s $arg ; #s + 1
}
return $s
}

puts [sum 1 2 3 4 5]
puts [sum 1 2 3 4]
puts [sum 1 2 3]
puts [sum 1 2]
puts [sum 1]
Arguments with default values/implicit arguments.
Implicit arguments in TCL procedures may have implicit values.
An Implicit arguments value is used if no explicit value is provided.

proc show {a {b 2} } {
puts $a
puts $b
}

show 10 20

proc add {a {b 2}} {


set c [expr $a + $b]
return $c
}

puts [add 2]

how to return mutliple values in the proc.


#the return command passes one value to caller.
there is often a need to return the mulitple values. in such cases we can make
return list.

proc arti {a b} {
set m1 [expr $a + $b]
set m2 [expr $a - $b]
set m3 [expr $a * $b]
set m4 [expr $a / $b]
return [list $m1 $m2 $m3 $m4]
}

puts [arti 20.0 10]

scope of the varibles in the proc:

local variables:
A varible decleared inside a procedure has a procedure scope/local varible.

proc show {} {
set a 10
puts $a
}

puts $a
show

global varibles:
Any variable which is declared outside the proc. we can
also use this varibale inside the proc.

set b 20

set b 20

proc scope {a} {


global b
set c [expr $a + $b]
return $c
}

puts "using global vaiable inside the proc:[scope 10]"

#set d [expr $b + $b]


#puts "global varible is used outside the proc:$d"

Recurison in the proc:

Recursion is way to call the procedure itself repeatedly.

Recurison is a way of declearing proc in which the proc being defined


is applied within its own definition.

In other word, a recurison function calls itself to do its job.

proc sumnum {a} {


if {$a > 0} {
return [expr $a + [sumnum [expr $a - 1]]]
} else {
return 0
}
}

puts [sumnum 5]

proc fac {a} {


if {$a > 0} {
return [expr $a * [fac [expr $a - 1]]]
} else {
return 1
}
}

puts "The factorial of the given number is:[fac 6]"

In Tcl, a "nested proc" is a procedure that is defined inside another procedure.


This means that the nested procedure is only visible and accessible within the
scope of the outer procedure. Once the outer procedure returns or is exited, the
nested procedure is no longer accessible.

proc outer_proc {} {
proc inner_proc {name} {
puts "Hello, $name!"
}
inner_proc "Bob"
}

outer_proc

what is array in tcl ?

In Tcl, an array is a data structure that stores a collection of values, which can
be accessed using an index. It is similar to a list or a dictionary in other
programming languages. An array is defined using the "array" command, followed by
the name of the array and its values.

array set numbers { 1 10 2 20 3 30}

set first_number $numbers(1)


puts $first_number
set second_number $numbers(2)
puts $second_number
set third_number $numbers(3)
puts $third_number

Array in TCL:

In TCL array is a data structure that stores a collection


of values under single variable name.

Array is systematic arragements of elements with the indices.

Each value in the array is associated with an index which


is a unique identifier.

the value in TCL array can be different types,such as


integers, strings or other arrays.

the follwing are some of the operators that can be used in TCL
arrays.

1. array set : creates or update an array with a list of values.

2. array get : retrieves values from an array and return them


as a list.

3.array size: it returns the number of elements in an array.

4. array names : it returns the names(indices) of all the


elements.

5.array exist : it returns 1 if array is exist else 0 otherwise.

6.array unset : removes an array and all its elements.

parray : is used to print the array.

syntax to create the array:


# create the array
array set colour {0 black 1 white 2 blue 3 orange 4 red}

#prints the entire array


#parray colour

#prints the 0 indicies array.


puts $colour(0)
array set colour {0 black 1 white 2 blue 3 orange 4 red}
#Retrieve all the values in the array as a list
parray colour

set myarray [array get colour]


puts $myarray

# get the number of elements in the array


array set colour {0 black 1 white 2 blue 3 orange 4 red}
set myarray [array size colour]
puts $myarray

# check if array is exists


array set colour {0 black 1 white 2 blue 3 orange 4 red}

if {[array exist colour]} {


puts "Array is exist"
} else {
puts "Array is not exist"
}

# how to delete or unintailaize the array


array set colour {0 black 1 white 2 blue 3 orange 4 red}

array unset colour

if {[array exist colour]} {


puts "Array is exist"
} else {
puts "Array is not exist"
}

# its prints all the indicies of the array


array set colour {a black b white 2 blue 3 orange 4 red}

set myarray [array names colour]


puts $myarray

# to get only the values of the array


array set colour {0 black 1 white 2 blue 3 orange 4 red}

foreach item [array names colour] {

puts $colour($item)

In TCL, all arrays by nature are associative.


Arrays are stored and retrieved without any
specific order.

Associative arrays have an index that is not necessarily a number.

the convential way to create the array:


array set colour {0 red 1 white 3 black 4 green }
array set colour {0 red 1 white 3 black 4 green }
#Before add the array element
parray colour

set colour(2) orange


#after adding the array element
parray colour

array set colour {0 red 1 white 3 black 4 green }


parray colour

set colour(0) orange


parray colour

array set colour {0 red 1 white 3 black 4 green }


parray colour
set colour(0) orange
parray colour

how to create the array indiviually:


set colour(0) red
set colour(1) white
set colour(2) black
parray colour

#how to delete particular element or unintalize


particular element array.

set colour(0) red


set colour(1) white
set colour(2) black
#parray colour

unset colour(1)
parray colour

Example 1: Numeric array

# Define an array of integers


array set numbers {
1 10
2 20
3 30
}

# Access elements of the array


set num1 $numbers(1) ;# num1 is 10
set num2 $numbers(2) ;# num2 is 20
set num3 $numbers(3) ;# num3 is 30

# Loop through all the elements of the array


foreach index [array names numbers] {
set value $numbers($index)
puts "numbers($index) = $value"
}
Example 2: Associative array# Define an associative array
array set person {
name "John Smith"
age 30
city "New York"
}

# Access elements of the array


set name $person(name) ;# name is "John Smith"
set age $person(age) ;# age is 30
set city $person(city) ;# city is "New York"

# Loop through all the elements of the array


foreach key [array names person] {
set value $person($key)
puts "$key = $value"
}

Example 3: Multi-dimensional array

# Define a 2D array of integers


array set matrix {
{0,0} 1
{0,1} 2
{1,0} 3
{1,1} 4
}

# Access elements of the array


set m00 $matrix(0,0) ;# m00 is 1
set m01 $matrix(0,1) ;# m01 is 2
set m10 $matrix(1,0) ;# m10 is 3
set m11 $matrix(1,1) ;# m11 is 4

# Loop through all the elements of the array


foreach indices [array names matrix] {
set value $matrix($indices)
puts "matrix($indices) = $value"
}

example:1
./file.tcl 1

output: day is monday

example:2
./file.tcl std_name sub

1. using the arguments we want to read student name and subject.


2. if we mentioned any student name and subject name.
3. based on the student name and subject name we have get the
marks particular subject.
4. print the error if stduent name and subject name
is not there in database.Array in TCL:

In TCL array is a data structure that stores a collection


of values under single variable name.
Array is systematic arragements of elements with the indices.

Each value in the array is associated with an index which


is a unique identifier.

the value in TCL array can be different types,such as


integers, strings or other arrays.

the follwing are some of the operators that can be used in TCL
arrays.

1. array set : creates or update an array with a list of values.

2. array get : retrieves values from an array and return them


as a list.

3.array size: it returns the number of elements in an array.

4. array names : it returns the names(indices) of all the


elements.

5.array exist : it returns 1 if array is exist else 0 otherwise.

6.array unset : removes an array and all its elements.

parray : is used to print the array.

syntax to create the array:

# create the array


array set colour {0 black 1 white 2 blue 3 orange 4 red}

#prints the entire array


#parray colour

#prints the 0 indicies array.


puts $colour(0)

array set colour {0 black 1 white 2 blue 3 orange 4 red}


#Retrieve all the values in the array as a list
parray colour

set myarray [array get colour]


puts $myarray

# get the number of elements in the array


array set colour {0 black 1 white 2 blue 3 orange 4 red}
set myarray [array size colour]
puts $myarray

# check if array is exists


array set colour {0 black 1 white 2 blue 3 orange 4 red}

if {[array exist colour]} {


puts "Array is exist"
} else {
puts "Array is not exist"
}

# how to delete or unintailaize the array


array set colour {0 black 1 white 2 blue 3 orange 4 red}

set a [array get colour]


puts $a

# its prints all the indicies of the array


array set colour {a black b white 2 blue 3 orange 4 red}

set myarray [array names colour]


puts $myarray

# to get only the values of the array


array set colour {0 black 1 white 2 blue 3 orange 4 red}

foreach item [array names colour] {

puts $colour($item)

In TCL, all arrays by nature are associative.


Arrays are stored and retrieved without any
specific order.

Associative arrays have an index that is not necessarily a number.

the convential way to create the array:


array set colour {0 red 1 white 3 black 4 green }

array set colour {0 red 1 white 3 black 4 green }


#Before add the array element
parray colour

set colour(2) orange


#after adding the array element
parray colour

array set colour {0 red 1 white 3 black 4 green }


parray colour

set colour(0) orange


parray colour

array set colour {0 red 1 white 3 black 4 green }


parray colour
set colour(0) orange
parray colour

how to create the array indiviually:


set colour(0) red
set colour(1) white
set colour(2) black
parray colour

#how to delete particular element or unintalize


particular element array.

set colour(0) red


set colour(1) white
set colour(2) black
#parray colour

unset colour(1)
parray colour

Example 1: Numeric array

# Define an array of integers


array set numbers {
1 10
2 20
3 30
}

# Access elements of the array


set num1 $numbers(1) ;# num1 is 10
set num2 $numbers(2) ;# num2 is 20
set num3 $numbers(3) ;# num3 is 30

# Loop through all the elements of the array


foreach index [array names numbers] {
set value $numbers($index)
puts "numbers($index) = $value"
}

Example 2: Associative array# Define an associative array


array set person {
name "John Smith"
age 30
city "New York"
}

# Access elements of the array


set name $person(name) ;# name is "John Smith"
set age $person(age) ;# age is 30
set city $person(city) ;# city is "New York"

# Loop through all the elements of the array


foreach key [array names person] {
set value $person($key)
puts "$key = $value"
}

Example 3: Multi-dimensional array

# Define a 2D array of integers


array set matrix {
{0,0} 1
{0,1} 2
{1,0} 3
{1,1} 4
}

# Access elements of the array


set m00 $matrix(0,0) ;# m00 is 1
set m01 $matrix(0,1) ;# m01 is 2
set m10 $matrix(1,0) ;# m10 is 3
set m11 $matrix(1,1) ;# m11 is 4

# Loop through all the elements of the array


foreach indices [array names matrix] {
set value $matrix($indices)
puts "matrix($indices) = $value"
}

example:1
tclsh p.tcl 2

output: day is monday

lassign $argv day

array set days {


1 monday
2 tuesday
3 wednesday
4 thrusday
5 friday
6 saturday
7 sunday
}

foreach key [array names days] {


if {$day == $key} {
puts "the day $day is:$days($key)"
} elseif {$day == 0 || $day > 7} {
puts "Error:day is not match"
exit
}
}

example:2
tclsh p.tcl std_name sub

1. using the arguments we want to read student name and subject.


2. if we mentioned any student name and subject name.
3. based on the student name and subject name we have get the
marks for the particular subject.
4. print the error if stduent name and subject name
is not there in database.

You might also like