1/08/2022

Regular Expression and File IO in TCL

In this article we are going to discuss about various string operations in TCL regular expressions such as find and replace method,  regular expression wildcard matching,  regular expression group matching,  and match status regular expression,  replace and match count. We will also discuss file input/output method, named code block, inbuilt mathematical commands and TCL Shell commands.

Various String Operations in TCL :

The concept of string in TCL is very similar to any other programming language. It is a stream of characters. We will  use code examples to understand the various string operations in TCL.

A Very Common Issue of  a String Token is that many times it may come with extra leading or trailing spaces. To remove this spaces , we can use the TRIM proc in TCL .

 We intentionally set a string with leading and trailing spaces  as :  

set statement " Fan is a student "

Then we can use the TCL procedure trim it as to remove the space from both sides as :

set statement [string trim $statement]

Here, we have saved memory consumption by saving the output back into same variable.

We can measure the length of a string and directly print on the screen as :

puts [string length $statement]

We can get the n-th character of a string by using the procedure "index" : 

puts [string index $statement 4]

puts [string index $statement end]

To get the position of the sub-string "is" with the entire string we can use the below procedures :

puts [string first "is" $statement]

puts [string last "is" $statement]

If we interchange the position  of string and sub-string , we cannot find it and it returns a value of "-1".

puts [string first $statement "is"]

 To extract a range of sub-string starting from one index till another we use the range operator :

puts [string range $statement 4 end]

This will give "is a student".

We can replace  a range of sub-string with a given one as :

puts [string replace $statement 9 end "professor"]

This will give output as  "Fan is a professor".

We can check for a match for a given sub-string for a existing string as :

puts [string match "*student" $statement] 


Here goes the entire code snippet :


And Here goes the output :



Regular Expressions (RegEx) in TCL :

The Regular Expression is common in every high level programming languages such as PERL, Shell, PHP , Python and TCL. The RegEX Engine  underneath any of these programming languages is same and hence follow the the same expression construction rules as :

Regular expressions can be expressed in just a few rules/meta-characters :

^ Matches the beginning of a string

$ Matches the end of a string

. Matches any single character

* Matches any count (0-n) of the previous character

+ Matches any count, but at least 1 of the previous character

[...] Matches any character of a set of characters

[^...] Matches any character *NOT* a member of the set of characters following the ^.

(...) Groups a set of characters into a subSpec.


We will use code examples to understand Regular expression matching in TCL.


Find and Replace : 

To find a RegEx inside a string we use TCL Procedure "regexp" and to Replace the matched sub-string with a target string , we use "regsub" .

In the below example we  carefully use a IF condition to check whether the  match is found , if found then we will replace with the target string .

The output goes like this :


Wild Card Matching :

The meta-characters which are used for construction a RegEx in TCL , can be used to do a wild-card matching as shown in eh below code example :

In this code , the string is used as a list to iterate on its each element. The wildcard expression "b.*n" means "b followed by any characters(-as signified by .) and any number of times(-as signified by *)" . Hence the entire RegEX for wild card matching is constructed and used.

The pair of curly-brackets creates a group.

The Output are "brown" and "brwn" as both of them satisfied the wild card match criteria. None of the other words in the sentence matches the criteria. 


Group Matching and Status : 

We can create one or more RegEx Groups using more than one pair of curly-brackets.  Here we are more focused on the match status which comes as a return value. And the matched strings, which we get using the below expression :

set sample "Where there is a will, There is a way."

set result [regexp {[a-z]+} $sample match]

Here the constructed group will match any characters in the range of a to z (-as signified by [a-z]) and at least once (-as signified by +) .  The variable result captures the status and the variable match captures the matching string. 

In the below code the grouping is extended to two :

 

And the code gives the output of  :


Group Matching & Meta-Characters Experiment :

In the previous section we have used the group {[a-z]+} as a regular expression. In this section we will do some experiments with this by changing it in various ways. We will change one thing at a time inside this group to observe the effect.

The Variants are :

{[a-z]*}  -----  This Matches none of the words from the string, as * matches 0 or more times.

{[A-Za-z]*} ----- This matches "Where" as we have included the Capital Letter Range with * remain unchanged.

{[A-Za-z]+} ----- This matches "Where" as we have included the Capital Letter Range and +  means 1 or more times.

{.*}  ----- This matches the entire string as . means any characters and * means 0 or more times.

{.+}  ----- This matches the entire string as . means any characters and + means 1 or more times.


We combine all the variations in a single code as :

Which gives the below output when we run it !


TCL RegEx : Direct Replace and Match Count 

We can use the regsub procedure without the if condition when we are certain about the presence . 

regsub "way" $sample "abundance" sample2

Here we have send the output to a separate string than the input string.

The TCL Expression "[regexp -all {[^ ]+} $sample]" matches all the words inside the string $sample and the switch -all gives the count .

The entire code can be written as below :

Which will give the output as   :


File I/O in TCL : Capturing Live Design Shell Data

In VLSI EDA Tools, the live design data analysis that you do get flashed into the live shell. Unless you do a hard copy of it in the disk, you are going to loose all them. This is the reason the File I/O is very important in VLSI EDA.

To open a file in read mode in TCL you simply have to write  a line of code like :

set fRead [open infile.txt r]

Here the file handle is fRead and the procedure of file I/O is open. Notice r at the last.

To open a file in write mode in TCL you simply have to write  a line of code like :

set fWrite [open outfile.txt w]

Here the file handle is fWrite and the procedure of file I/O is open. Notice w at the last.

Then we use a while loop till the end of the file is reached as :
while { ![eof $fRead]} { .......................}
Inside which we read the line like :
set line [gets $fRead]

And we can write a line  like :


puts $fWrite $line
Then finally we close both the file handles as :
close $fRead
close $fWrite

The entire code is provided here for your trial :





Code Reuse in TCL : Named Code Block :


We can use a block of code repeatedly in an long TCL code. But first we have to define it. 
We can define a code block using set command and use it later by eval command as shown in the below example :



Inbuilt Mathematical Functions :


TCL has few inbuilt mathematical functions that are ready to use :
Abs  acos  asin   atan  atan2   bool  cos cosh entier  exp fmod  hypot isqrt log log10  max min pow rand  round  sin sinh  sqrt srand tan tanh wide



TCLSH Shell Commands:

Some TCLSH  Shell Commands that you may use similar to BASH/TCSH : 

cd – Change Directory in side TCLSH
history – to see commad history
exit – Exit the TCLSH prompt
error – Issue Error through your TCL Code  [ error “here is your error message”]

This Entire Article is well Narrated in the Below Two Videos :






Courtesy : www.pngegg.com