Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What is the syntax of calling a procedure in TCL?

Status
Not open for further replies.

sun_ray

Advanced Member level 3
Joined
Oct 3, 2011
Messages
772
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Activity points
6,828
What is the syntax of calling a procedure in tcl?

Can a procedure be called inside a if-else condition in tcl?

Regards
 

Re: procedure in tcl language

Hai sun ray,
Below is a procedure and is called inside if statement

proc sum {arg1 arg2} {
set x [expr {$arg1 + $arg2}];
return $x
}
set x 1
if {$x == 1} {
puts " The sum of 2 + 3 is: [sum 2 3]\n\n"
}


"sum" is a pocedure name..
called inside if statement as
syntax:- sum 2 3

using this method we can call inside if else statement..
Feel free to ask if u have doubt in this...:razz:
 
Re: procedure in tcl language

proc kk {pr_to} {
if { $pr_to=="inv" } then {
set gate inv
} elseif { $pr_to=="nor" } then {
set gate nor
} else {
set gate none
}
}

I wrote the above procedure and called the procedure in the following way:

kk inv

I called in the above way the procedure. Now I expect that variable gate to be set as inv. But I do not find that. I find variable gate to be unset. Where is the flaw in this procedure?

Regards
 

Re: procedure in tcl language

proc kk {pr_to} {
if { $pr_to eq "inv" } then {
set gate inv
#puts "inv"
} elseif { $pr_to=="nor" } then {
set gate nor
#puts "nor"
} else {
set gate none
#puts "none"
}
puts $gate
}
kk inv

Its working fine and its also setting.. u need to declare as global if u puts outside procedure..
if u call inside this u will get the gate value perfectly..

I think have tried to use the variable $gates outside this procedure.. So mention as global if u need outside...

- - - Updated - - -

proc kk {pr_to} {
global gate
if { $pr_to eq "inv" } then {
set gate inv
#puts "inv"
} elseif { $pr_to=="nor" } then {
set gate nor
#puts "nor"
} else {
set gate none
#puts "none"
}
#puts $gate
}
kk inv
puts $gate

Here is ur code have inv value in gate outside procedure..
This is k or u need more help..:razz:

- - - Updated - - -
 

Re: procedure in tcl language

proc kk {pr_to} {
if { $pr_to eq "inv" } then {
set gate inv
#puts "inv"
} elseif { $pr_to=="nor" } then {
set gate nor
#puts "nor"
} else {
set gate none
#puts "none"
}
puts $gate
}
kk inv

Its working fine and its also setting.. u need to declare as global if u puts outside procedure..
if u call inside this u will get the gate value perfectly..

I think have tried to use the variable $gates outside this procedure.. So mention as global if u need outside...

- - - Updated - - -

proc kk {pr_to} {
global gate
if { $pr_to eq "inv" } then {
set gate inv
#puts "inv"
} elseif { $pr_to=="nor" } then {
set gate nor
#puts "nor"
} else {
set gate none
#puts "none"
}
#puts $gate
}
kk inv
puts $gate

Here is ur code have inv value in gate outside procedure..
This is k or u need more help..:razz:

- - - Updated - - -

Why are you potting # puts "nor" like command? The # is to comment a line.
 

Re: procedure in tcl language

ya its a comment i just used to check whether it passed to all or only for inv case.... checked if loop..

It does nothing..
 

Re: procedure in tcl language

Is it that each line in tcl needs to be ended with ; ?

When is it necessary to put : at the end of a tcl command?

You are right that I tried to put the $gate outside the procedure.
 

Re: procedure in tcl language

ya sun_ray u got it.....

no tcl code should not need ; for end of the line like c or perl..

we can write simply as above code..
 

Re: procedure in tcl language

ya sun_ray u got it.....

no tcl code should not need ; for end of the line like c or perl..

we can write simply as above code..

What is the difference between $env(variable_name) and $variable_name?

- - - Updated - - -

I want to jpin the content of two variable. How can I do that? Please provide the command.

How can I call a procedure (proc) if I want to call a procedure in tcl with the arguments as some variable ( var1 and var2)?

Can I call as proc $var1 $var2? Or as follows

proc [puts $var1] [puts $var2]
 
Last edited:

Re: procedure in tcl language

Hai sun_ray,,


<<1>>

$env(variable_name) is environmental variable
$variable_name is a normal variable


enviromental variable can be setted in a terminal window itself..
using below command..Here gate_name is environmental variable..

setenv gate_name inv

just type the above line in terminal. We can call this environmental variable in any language according to their syntax.

In tcl we call it by

$env(gate_name)

Normal variables can be setted and called simply as

set gate inv
$gate

<<2>>
consider

a variable has value 1
b variable has value 2

proc_name 1 2

if u give value directly u can use this..

but if u want to pass

proc_name $var1 $var2

u can also pass variable like this..

proc_name [puts $a] [puts $b]

Its a evaluate funciton [].. What u r doing here is printing "a" value and "b" value

Its equal to

proc_name "1" "2"



The main usage of [] is to perform some operation.. then send the value to the function similar to eval function:razz:
 
Re: procedure in tcl language

Hai sun_ray,,


<<1>>

$env(variable_name) is environmental variable
$variable_name is a normal variable


enviromental variable can be setted in a terminal window itself..
using below command..Here gate_name is environmental variable..

setenv gate_name inv

just type the above line in terminal. We can call this environmental variable in any language according to their syntax.

In tcl we call it by

$env(gate_name)

Normal variables can be setted and called simply as

set gate inv
$gate

<<2>>
consider

a variable has value 1
b variable has value 2

proc_name 1 2

if u give value directly u can use this..

but if u want to pass

proc_name $var1 $var2

u can also pass variable like this..

proc_name [puts $a] [puts $b]

Its a evaluate funciton [].. What u r doing here is printing "a" value and "b" value

Its equal to

proc_name "1" "2"



The main usage of [] is to perform some operation.. then send the value to the function similar to eval function:razz:

But in my code var1 and var 2 are environment variables. So if I call the procedure in the following way, it is not getting called:

proc_name [puts $env(var1)] [puts $(var2)]

Even if I call in the following way it is not getting called:
proc_name [puts $var1] [puts $var2]


Can you please let me know what can be the issue? For information, in my case var1 and var2 has been declared as environment variable in another C Shell script in which this tcl script is being called.


I have another question as follows:

What is an environment variable? What is the need of them?
 

Re: procedure in tcl language

Hai sunray,

sum [puts $env(var1)] [puts $env(var2)]

> This in not the right way to call


This is easy.. Avoid puts in ur statement.. u can call by simply giving as below

proc_name $env(var1) $env(var2)

Mistakes Done :-

> proc_name [puts $env(var1)] [puts $(var2)] - Avoid using puts for env variable while passing to procedure
> proc_name [puts $var1] [puts $var2] - This will call only local variable..

environmental variable are used for the child process in d sense where ever u want..

Main usage:-
we can get this environmental variable value in any language like perl,tcl,shell etc..,:p
 

Re: procedure in tcl language

environmental variable are used for the child process in d sense where ever u want..

Main usage:-
we can get this environmental variable value in any language like perl,tcl,shell etc..,:p

What is child process? What do you mean by 'd sense'?

Are environment variable basically UNIX/LINUX or operating system variable?


Another query as follows

You wrote that

proc_name $env(var1) $env(var2)

is the right way tocall.
But in the above way how $env(var1) is being recognized/evaluated without a puts? Is it that this is the syntax of tcl to call in this way as follows
proc_name $env(var1) $env(var2)?


But you also wrote that we can call in the following manner for tcl variables:

proc_name [puts $a] [puts $b]

Regards
 
Last edited:

Re: procedure in tcl language

HAi sun_ray,

child process is nothing its a derived process..

in d sense = in the sense

proc_name $env(var1) $env(var2)

is one of the right way to call.

In this puts function is not needed.. Its unnecessary because its just puts the value.. In most cases we will not assign parameter value using puts other than string value..

proc_name [puts $a] [puts $b]

u can also do like this... but its an extra thing u r adding.. So it will increase the run time because it will call to puts function and then it will send to procedure..

Now refer 10th post too...
 

Re: procedure in tcl language

proc_name [puts $a] [puts $b]

u can also do like this... but its an extra thing u r adding.. So it will increase the run time because it will call to puts function and then it will send to procedure..

.

Do you want to mean that even if a and b are environment variable we can call the procedure in the following way?

proc_name [puts $a] [puts $b]?



I have a file that contains lines which contain the word 'necessary' and in the same file there are lines which contain the word 'fatal'. Can you please provide a script that can generate two files out of this single files in such a way that one of the two files will contain only lines contianing the word 'necessary' and another file will contain all lines containing the word 'fatal'?

Regards

- - - Updated - - -

proc_name [puts $a] [puts $b]

u can also do like this... but its an extra thing u r adding.. So it will increase the run time because it will call to puts function and then it will send to procedure..

.

Do you want to mean that even if a and b are environment variable we can call the procedure in the following way?

proc_name [puts $a] [puts $b]?



I have a file that contains lines which contain the word 'necessary' and in the same file there are lines which contain the word 'fatal'. Can you please provide a script that can generate two files out of this single files in such a way that one of the two files will contain only lines containing the word 'necessary' and another file will contain all lines containing the word 'fatal'?

Regards
 

Re: procedure in tcl language

Hai sunray,

proc_name [puts $a] [puts $b]

No u cant this will call only local variable

U need the code in tcl or perl????:razz:
 

Re: procedure in tcl language

Can you please provide the script in tcl?
 

Re: procedure in tcl language

Hai sunray,

set file1 [open "./[lindex $argv 0]" r]
set file_fatal [open "./fatal" w]
set file_necessary [open "./necessary" w]
while {[gets $file1 line] >= 0} {
if { [regexp {fatal} $line] } {
puts $file_fatal $line
}
if { [regexp {necessary} $line] } {
puts $file_necessary $line
}
}
close $file1
close $file_fatal
close $file_necessary

save this code in a file as fatal_necessary.tcl

Run the code as

tclsh fatal_necessary.tcl filename

Have fun.:p
 
Re: procedure in tcl language

What is this $argv 0 doing in the first line?

What is thhis "./fatal" w doing in the second line?
 

Re: procedure in tcl language

$argv 0 is getting the filename Its getting command line arguments..


"./fatal" w Its opening a file named fatal for write mode........
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top