Monday, December 7, 2015

File handling

File Handling :

File names could be specified in Unix format.
In order to work with a file, first one has to open a channel to the file...this is also called handle
Once the channel is opened, TCL retruns a channelId which is used to work with the files
Typical steps to be followed while working with files:
Step 1: Check whether the file specified exists or not
Step 2: Open a channel pointing to the file...TCL returns an ID
Step 3: Use the ID to work with the file
Step 4: Close the channel after use

File Open :

"open" could be used to open a channel to a file. This command returns the file-handle (channelID) which could be used to work with the files.
Syntax:
open fileName ?access mode?
where
fileName - is the name of the file to be opened for use
access mode - either r, r+, w, w+, a or a+. These are the access modes (similar to C language)
"open" opens a channel to the file specified by the fileName with the access permissions specified by the access mode. Default is "r".
A channel once opened, remains open until specifically closed.

File Close :

"close" is used to close a channel previously opened using the "open" command.
Syntax:
close fileID
where
fileID - is the channel ID of the file opened


Example :




some of the useful file operations are: (no need to open a channel for these operations)
file exists      --> checks if the specifie file exists or not
file dirname     --> returns the parent directory of the specified file
file extension   --> returns the extension (suffix) of the specified file (incl. dot)
file isfile      --> returns 1 if “name” is a normal file
file isdirectory --> returns 1 if “name” is a directory
file executable  --> returns 1 if “name” has executable permissions
file readable    --> returns 1 if “name” has read permissions
file writable    --> returns 1 if “name” has write permissions
file tail        --> returns the last pathname component of “name”

file rootname    --> returns all but the extension of the “name”

procs

procedures are supported by TCL :

Procedures return to the calling program through "return" statement
Every procedure must have a corresponding return statement. This however is not strictly required.
Once they are created, procedures could be used just as any other TCL command
Procedures are declared with the help of keyword "proc"

Each procedure may have a set of formal parameters and some variables - local and global
Syntax:
proc proc_name { formal parameters } {
  statement1
  statement2
  :
  return
}
Example:
proc plus { a b } {
  expr $a + $b
  return
}

Syntax:
proc name argList body
where,
name - is the procedure name
argList - List of arguments. If a procedure takes more than one argument, they should be specified within braces.
body - body of the procedure
Example:

% proc proc_a { a b c } { body of procedure }
% proc proc_b b { body of procedure }
Once created, a procedure could be invoked/ called just by its name
Example: proc_a { $a $b $c }, proc_b { $b } w.r.t. The above example
Every procedure has its own set of local variables.
Any two procedures may have variables with the same names. The values of the variables do not get affected since they are not visible outside the procedure block.

"global" could be used to declare the scope of some variables as global
"global" should be used inside a procedure
Syntax:
global varName ?varName ...?
where
varName - is the name of variable whose scope has to be
extended beyond a procedure. More than one variable could be 
specified.
"global" binds variable names "varName"... to global variables. References to these names will refer to global variables instead of local variables for the duration of the current procedure.




Example 1:

Example 2: Simple tree traversal iterative procedure.

Example 3 :

Sunday, December 6, 2015

continue and break


  • Continue and Break statements
  • Continue statement will terminate the current execution of the loop and start the next iteration.
  • Break statement will terminate the execution of the loop and comes out of the loop.
  • Continue and Break statements are very important in real life implementation.


  •  For example, you might run placer or some extraction which is running for like a very long time (say 48 hours) and you do not want to lose your database with all the fixes you can use break and come out of the loop. Another example is if you do not want to execute the timer update on every iteration, but want to execute in every other iteration.

 Example 1:





Control Structures

Control Structures : (Under construction)

In general execution of the program would be sequential, if the programmer needs to change the flow of execution, he can very well do using certain tcl constructs. We will discuss about them in this section. Following are the different constructs which are available in TCL:

  • if - else & if - elseif - else structure
  • while structure
  • foreach  structure
  • for structure
  • switch





  • if - else & if - elseif - else structure
     If the condition is evaluated true, command1 and command2 will be executed.
     Syntax :
       if { condition } {
       command1
       command2
       }


     You can use if-else structure as well. i.e. if the condition is true do this and if it is false do that.
    Syntax :
     if { condition } {
       command1
       command2
      } else {
        command3
        command4
     }

You can use if-elseif-else and even nested if within each group.

If condition1 is evaluated to true, command_m would be executed, else if condition2 is evaluated to true command_n. If neither the condition evaluates to true, command_p would be executed



   if { condition1 } {
  command_m  
 } elseif { condition2 } { 
  command_n    
} else {  
command_p  
  }
Example 1:



While loop :

  • While the condition evaluates to true iterate over the loop again and again till it evaluates to false. Once it evaluates to false come out of the loop.



while { condition } {
statement1;statement2;updation; }


Example 2:


For loop :
It is similar to the while loop except that you can have a flexibility to  initialize the loop iterator and also update the iterator in the same line.


for { initiliaze } { condition } { updation } {
command1;command2;..}

Example 3:


foreach control structure:
It loops over a set of variables in the list and executes operations on each variable. It is the most commonly used control structure in the TCL.


foreach var <list> {

execute operation on var;
statement1;
statement2;
.
.
}

Example 4:


TCL arrays

TCL array: (Under construction)

  • Array is a collection of variables.
  • Associative  -> abstract data type composed of a collection of key value pairs. 
  • Key value pairs are not ordered.
  • Each variable can hold any value or an array.

How to create an array in tcl?
  • We can create an array using set or "array set"
Array operations :
Different operations associated with array


array exists
array get
array names
array set
array size
array unset
array startsearch
array nextelement
array anymore
array donesearch
array statistics



Output: 
sat and Saturday are same
wed and Wednesday are same
fri and Friday are same
thu and Thursday are same
sun and Sunday are same
tue and Tuesday are same
mon and Monday are same

Note: Observe that key and value pairs are not ordered.

Example:2

Output:
4 is associated to  Thu
5 is associated to  Fri 
1 is associated to  Mon 
6 is associated to  Sat 
2 is associated to  Tue 
7 is associated to  Sun 
3 is associated to  Wed

All array operations are listed in this example
Example:3


Example:4


TCL upvar command usage

upvar, a built-in Tcl command, links a variable to some other variable at the specified level of evaluation.

i.e If you want to change the value of variable from with in the procedure we can use upvar to do that.

Example:
proc upvar_demo {var1 var2} {
upvar $var1 procvar1
upvar $var2 procvar2

set procvar1 TCL
set procvar2 Script

}

set outvar1 Hello
set outvar2 World

puts "Values of variables before executing the procedure"
puts "outvar1 : $outvar1"
puts "outvar2 : $outvar2"

upvar_demo outvar1 outvar2
puts "Values of variables after executing the upvar procedure"
puts "outvar1 : $outvar1"
puts "outvar2 : $outvar2"

#Output :
Values of variables before executing the procedure
outvar1 : Hello
outvar2 : World

Values of variables after executing the upvar procedure
outvar1 : TCL


outvar2 : Script