tcl language ppt to learn the tcl go through it

polimerapramodkumar1 134 views 86 slides Dec 16, 2024
Slide 1
Slide 1 of 86
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86

About This Presentation

tcl language


Slide Content

Corporate Presentation 1 © Radiant Semiconductors Confidential

TCL Tool Command Language

Which language Computer understand ? Machine language consists of binary numbers (0s and 1s), while humans understand high-level languages, such as English. Then how will Humans communicate with the computer ? We use compilers and interpreters to facilitate communication between humans and computers.

Difference between Compiler and Interpreter

What is TCL? TCL stands for Tool Command Language. It was designed by John Ousterhout of the University of California, Berkeley in 1988. It's an opensource multi-purpose interpreted powerful dynamic scripting language. It aims at providing ability for the programs to interact with other programs. TCL is fit for both the smallest and largest programming task. Tcl was developed initially for Unix. It was then ported to Windows, DOS, OS/2, and Mac OSX. Tcl is much similar to other unix shell languages like Bourne Shell ( Sh ), the C Shell ( csh ), the Korn Shell ( sh ), and Perl. Widely used in various domains in VLSI Design and Verification.

TCL in VLSI Front – End Synthesis Tool Back-End Placement and Route Tool ( PnR ) Static Timing Analysis Tool (STA) Reliability Analysis Tool (EM-IR) Scripted Waveform Verification in Analog Design for Testability Tool (DFT)

Why Should we learn TCL for VLSI Design Efficiency: TCL automation dramatically reduces manual effort, accelerating design processes. Accuracy: By automating repetitive tasks, TCL minimizes the risk of human errors, leading to more reliable designs. Flexibility: TCL's versatility adapts to the evolving requirements of VLSI projects, allowing us to quickly adjust to changing design needs. Competitive Edge: TCL proficiency is a vital skill in the VLSI industry, giving you a competitive advantage in job markets.

Why TCL? TCL is not just another programming language; It forms the backbone of VLSI design automation specifically for Physical Design Engineers. It empowers engineers to automate complex and repetitive tasks, improving efficiency, accuracy, and productivity in semiconductor design. In many EDA [Electronics Design Automation] tools we will use TCL for scripting. Simplest syntax Cross-platform availability Data structure: TCL provides built in data structures such as lists, dictionaries and arrays. Control Structures: Use built-in control structures such as if, for, foreach, while, switch etc Provides facility for creating GUIs Provides enough programmability for complex scripts Rapid development Open source – Anyone may, without charge, download and inspect the source code to Tcl .

Some key concepts and their importance in VLSI Understanding TCL Variables: Variables in TCL are essential for storing data, enabling us to manipulate values, strings, and numbers effortlessly. They are the building blocks of TCL scripts, allowing us to work with data effectively. 2. Mathematical and Boolean Operations: TCL offers a strong set of mathematical and Boolean operators. These operations are invaluable when dealing with calculations, comparisons, and logic in VLSI Physical Design. 3. List Management and Manipulation: Lists are versatile data structures in TCL, and they find applications in various aspects of VLSI, from managing design constraints to handling simulation scenarios. TCL's list manipulation capabilities are essential for data organization and analysis.

4. File Handling and Manipulation: Design Engineers have to work with numerous design files in the VLSI. TCL excels in file manipulation tasks, making it effortless to read, write, and process files, facilitating data exchange with EDA tools. 5. Variable Manipulation: TCL's variable manipulation features allow engineers to efficiently modify and adapt data to meet specific design requirements. This is invaluable when working with design constraints, parameters, and configurations. 6. Control Statements : TCL provides a range of control statements like loops and conditional statements. These enable engineers to create flexible and adaptive scripts, which are critical for automating complex VLSI workflows.

7. Procedures: In VLSI, scripts must often be modular and reusable. Procedures in TCL allow engineers to encapsulate functionality into functions, making code more maintainable and promoting code reuse. 8. Array and Dictionary Manipulation: Arrays and dictionaries are advanced data structures in TCL that prove useful when dealing with complex data organization and retrieval in VLSI. 9. Regular Expressions: Regular expressions in TCL are indispensable for pattern matching and text processing. They are valuable in parsing log files, generating reports, and extracting critical information.

Environment Setup To set up your environment for Tcl , you require two software applications available on your computer: Text Editor Tcl Interpreter Text Editor Used to type the program. Ex: Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi. Name and version of a text editor can vary on different operating systems. The files you create with your text editor are called source files and contain program source code. The source files for Tcl programs are named with the extension  ". tcl " . Tcl Interpreter A small program that enables you to type Tcl commands and have them executed line by line. It stops execution of a tcl file, in case, it encounters an error unlike a compiler that executes fully.

Basic Syntax Basic syntax for a Tcl command command arg1 arg2 …... White space to separate command name and its arguments Newline or semicolon to terminate a command # for a comment Tcl is case sensitive

Basic Syntax Comments Comments are like helping text in your Tcl program and the interpreter ignores them. Comments can be written using a hash (#) sign in the beginning. #!/usr/bin/tclsh # my first program in Tcl puts "Hello World!" -> Hello World!

Basic Syntax First Tcl Program All Tcl files will have an extension, i.e., . tcl . So, put the following source code in a test.tcl file. #!/usr/bin/tclsh puts "Hello, World!" Assuming, Tcl environment is setup correctly; let's run the program after switching to file's directory and then execute the program using − $ tclsh test.tcl -> Hello, World! In Tcl , we use new line or semicolon to terminate the previous line of code. But semicolon is not necessary, if you are using newline for each command.

Basic Syntax Comments Multiline or block comment is written using 'if' with condition '0'. #!/usr/bin/tclsh if 0 {
my first program in Tcl program
Its very simple
}
puts "Hello World!" -> Hello World! Inline comments use ;#. #!/usr/bin/tclsh puts "Hello World!" ; # my first print in Tcl program -> Hello World!

Basic Syntax Identifiers A Tcl identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, dollars ($), and digits (0 to 9). Tcl does not allow punctuation characters such as @, and % within identifiers. Tcl is a  case sensitive language. Thus  Manpower  and  manpower  are two different identifiers in Tcl .

Basic Syntax Whitespace in Tcl A line containing only whitespace, possibly with a comment, is known as a  blank line , and a Tcl interpreter totally ignores it. Whitespace is the term used in Tcl to describe blanks, tabs, newline characters, and comments. Whitespace separates one part of a statement from another and enables the interpreter to identify where one element in a statement, such as puts, ends and the next element begins.

Basic Syntax #!/usr/bin/tclsh puts "Hello World!" There must be at least one whitespace character (usually a space) between “puts” and "Hello World!" for the interpreter to be able to distinguish them. #!/usr/bin/tclsh puts [ expr 3 + 2 ] ;# print sum of the 3 and 2 -> 5 No whitespace characters are necessary between 3 and +, or between + and 2; although, you are free to include some if you wish for the readability purpose.

TCL Commands Tcl is a Tool command language, commands are the most vital part of the language. Tcl commands are built in-to the language with each having its own predefined function. These commands form the reserved words of the language and cannot be used for other variable naming.  The advantage with these TCL commands is that, you can define your own implementation for any of these commands to replace the original built-in functionality. Each of the TCL commands validates the input and it reduces the work of the interpreter. TCL command is actually a list of words, with the first word representing the command to be executed. The next words represent the arguments. In order to group the words into a single argument, we enclose multiple words with "" or {}. Syntax of Tcl command: commandName {argument1 argument2 ... argumentN }

TCL Commands Example of TCL command: Tcl command with one argument − puts "Hello, world!" -> Hello, world! In the above code, ‘puts’ is the Tcl command and "Hello World" is the argument1. We have used "" to group two words. Tcl command with two arguments − puts stdout "Hello, world!"   -> Hello, world! In the above code, ‘puts’ is the Tcl command, ‘ stdout ’ is argument1, and "Hello World" is argument2. Here, stdout makes the program to print in the standard output device.

TCL Commands Command Substitution Square brackets are used to evaluate the scripts inside the square brackets.  puts [expr 1 + 6 + 9] -> 16 Variable Substitution $ is used before the variable name and this returns the contents of the variable.  set a 3
puts $a -> 3 Backslash Substitution Commonly called escape sequences; with each backslash, followed by a letter having its own meaning. puts "Hello\ nWorld " -> Hello World Some other backslash substitution commands are \n, \t, \b etc.

TCL Data Types The primitive data-type of Tcl is string and often we can find quotes on Tcl as string only language. These primitive data-types in turn create composite data-types for list and associative array. In Tcl , data-types can represent not only the simple Tcl objects, but also can represent complex objects such as handles, graphic objects and I/O channels.

TCL Data Types Simple Tcl Objects In Tcl , whether it is an integer number, boolean , floating point number, or a string, when you want to use a variable, you can directly assign a value to it, there is no step of declaration in Tcl . There can be internal representations for these different types of objects. It can transform one data-type to another when required. set myVariable 18
puts $ myVariable -> 18 If we try to make an arithmetic on the variable, it is automatically turned to an integer. set myVariable 18
puts [expr $ myVariable + 6 + 9] ->   33

TCL Data Types String Representations Unlike other languages, in Tcl , you need not include double quotes when it's only a single word. set myVariable hello puts $ myVariable -> hello When we want to represent multiple strings, we can use either double quotes or curly braces.  set myVariable "hello world" puts $ myVariable set myVariable {hello world} puts $ myVariable -> hello world
hello world

TCL Data Types List List is nothing but a group of elements. A group of words either using double quotes or curly braces can be used to represent a simple list. set myVariable { red green blue } puts [ lindex $ myVariable 2 ] set myVariable "red green blue" puts [ lindex $ myVariable 1 ] -> blue
green

TCL Data Types Associative Arrays Associative arrays have an index (key) that is not necessarily an integer. It is generally a string that acts like key value pairs. set marks( english ) 80
puts $marks( english )
set marks(mathematics) 90
puts $marks(mathematics) -> 80 90

TCL Data Types Handles Tcl handles are commonly used to represent files and graphics objects. These can include handles to network requests and also other channels like serial port communication, sockets, or I/O devices. -> set myfile [open "filename" r]

In Tcl , there is no concept of variable declaration. Once, a new variable name is encountered, Tcl will define a new variable. Variable Naming The name of variables can contain any characters and length. You can even have white spaces by enclosing the variable in curly braces, but it is not preferred. The set command is used for assigning value to a variable.  Syntax set variableName value set variableA 10 set { variable B } test
puts $ variableA puts $ { variable B } -> 10
test TCL Variables

Dynamic Typing Tcl is a dynamically typed language. The value of the variable can be dynamically converted to the required type when required. set variableA "10" puts $ variableA set sum [ expr $ variableA + 20 ]; puts $sum -> 10
30 TCL Variables

Mathematical Expressions The default precision of Tcl is 12 digits. In order to get floating point results, we should add at least a single decimal digit. set variableA "10" set result [ expr $ variableA / 9 ]; puts $result set result [ expr $ variableA / 9.0 ]; puts $result set variableA "10.0" set result [ expr $ variableA / 9 ]; puts $result ->1
1.1111111111111112
1.1111111111111112 TCL Variables

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Tcl language is rich in built-in operators and provides the following types of operators − 1. Arithmetic Operators 2. Relational Operators 3. Logical Operators 4. Bitwise Operators 5. Ternary Operator TCL Operators

Arithmetic Operators Following table shows all the arithmetic operators supported by Tcl language. Assume variable ‘A’ holds 10 and variable ‘B’ holds 20, then − TCL Operators Operator Description Example + Add two operands A + B will give 30 - Subtracts second operand from the first A – B will give -10 * Multiplies both operands A * B will give 200 / Divides numerator by denominator B / A will give 2 % Modulus operator and remainder of after an integer division B % A will give 0

Relational Operators Following table shows all the relational operators supported by TCL language. Assume variable ‘A’ holds 10 and variable ‘B’ holds 20, then − TCL Operators Operator Description Example == Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true. != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

Logical Operators Following table shows all the relational operators supported by TCL language. Assume variable ‘A’ holds 1 and variable ‘B’ holds 0, then − TCL Operators Operator Description Example && Called Logical AND operator. If both the operands are non-zero, then condition becomes true. (A && B) is false. || Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. (A || B) is true. ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true.

Bitwise Operators Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ are as follows − TCL Operators p q p & q p | q p ^ q 1 1 1 1 1 1 1 1 1 1

Assume if A = 60; and B = 13; now in binary format they will be as follows − A = 0011 1100 B = 0000 1101 ---------------------- A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 The Bitwise operators supported by Tcl language are listed in the following table. Assume variable  A  holds 60 and variable  B  holds 13, then − TCL Operators Operator Description Example & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12, which is 0000 1100 | Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61, which is 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49, which is 0011 0001 << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240, which is 1111 0000 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15, which is 0000 1111

Ternary Operator TCL Operators Operator Description Example ?: Ternary If Condition is true? Then value X : Otherwise value Y

Following table shows all the relational operators supported by TCL language. Assume variable ‘A’ holds 10 and variable ‘B’ holds 20, then − TCL Operators Operator Description Example == Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true. != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

Following table shows all the relational operators supported by TCL language. Assume variable ‘A’ holds 10 and variable ‘B’ holds 20, then − TCL Operators Operator Description Example == Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true. != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

Operators Precedence in TCL Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others. Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first. TCL Operators

Operators Precedence in TCL TCL Operators Category Operator Associativity Unary + - Right to Left Multiplicative * / % Left to Right Additive + - Left to Right Shift << >> Left to Right Relational < <= > >= Left to Right Bitwise AND & Left to Right Bitwise XOR ^ Left to Right Bitwise OR | Left to Right Logical AND && Left to Right Logical OR || Left to Right Ternary ?: Right to Left

A loop statement allows us to execute a statement or group of statements multiple times. Tcl language provides the following types of loops to handle looping requirements: TCL LOOPS Loop Type Description While loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. For loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. Nested loops Uses one or more loop inside any another while, for or do..while loop.

WHILE LOOP A  while  loop statement in Tcl language repeatedly executes a target statement as long as a given condition is true. Syntax while {condition} {
statement(s)
} Here,  statement(s)  may be a single statement or a block of statements. The  condition  may be any expression, and true is any nonzero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop. TCL LOOPS

WHILE LOOP set a 10 #while loop execution while { $a < 20 } { puts "value of a: $a" incr a } Output: value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19 TCL LOOPS

FOR LOOP A  for  loop is a repetition control structure that allows you to efficiently write a code that needs to be executed for a specific number of times. Syntax for {initialization} {condition} {increment} {
statement(s);
} Flow of control in a for loop − The  initialization  step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears. Next, the  condition  is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the  for  loop. After the body of the  for  loop executes, the flow of control jumps back up to the  increment  statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the  for  loop terminates. TCL LOOPS

FOR LOOP # for loop execution for { set a 10 } { $a < 20 } { incr a } { puts "value of a: $a" } Output value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19 TCL LOOPS

NESTED LOOP Tcl allows to use one loop inside another loop. Syntax The syntax for a  nested for  loop statement in Tcl − for {initialization} {condition} {increment} {
for {initialization} {condition} {increment} {
statement(s);
}
statement(s);
} The syntax for a  nested while loop  statement in Tcl language is as follows − while {condition} {
while {condition} {
statement(s);
}
statement(s);
} A final note on loop nesting is that you can put any type of loop inside of any other type of loop. For example, a for loop can be inside a while loop or vice versa. TCL LOOPS

NESTED LOOP A nested for loop to find the prime numbers from 2 to 100 − set j ; for { set i 2 } { $ i < 100 } { incr i } { for { set j 2 } { $j <= [ expr $ i / $j ] } { incr j } { if { [ expr $ i % $j ] == } { break } } if { $j >[ expr $ i / $j ] } { puts "$ i is prime" } } TCL LOOPS

Infinite Loop A loop becomes infinite loop if a condition never becomes false. The  while  loop is traditionally used for this purpose. By leaving the conditional expression as 1, an endless loop can be made. while {1} {
puts "This loop will run forever."
} When the conditional expression is absent, it is assumed to be true. Tcl programmers more commonly use the while {1} construct to signify an infinite loop. NOTE  − You can terminate an infinite loop by pressing Ctrl + C keys. TCL LOOPS

Loop Control Statements Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. TCL LOOPS Control Statement Description Break statement Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. Continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

BREAK STATEMENT The  break  statement in Tcl language is used for terminating a loop. When the  break  statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. If you are using nested loops (i.e., one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the next line of code after the block. Syntax break; TCL LOOPS

BREAK STATEMENT set a 10 # while loop execution while { $a < 20 } { puts "value of a: $a" incr a if { $a > 15 } { # terminate the loop using break statement break } } OUTPUT value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15 TCL LOOPS

CONTINUE STATEMENT The  continue  statement in Tcl language works somewhat like the  break  statement. Instead of forcing termination, however,  continue  forces the next iteration of the loop to take place, skipping any code in between. For the  for  loop,  continue  statement causes the conditional test and increment portions of the loop to execute. For the  while  loop,  continue  statement passes the program control to the conditional tests. Syntax continue; TCL LOOPS

CONTINUE STATEMENT set a 10 # do loop execution while { $a < 20 } { if { $a == 15 } { #skip the iteration incr a continue } puts "value of a: $a" incr a } OUTPUT value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19 TCL LOOPS

The primitive data-type of Tcl is string and often we can find quotes on Tcl as string only language. These strings can contain alphanumeric character, just numbers, Boolean, or even binary data. Tcl uses 16 bit unicode characters and alphanumeric characters can contain letters including non-Latin characters, number or punctuation. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false. STRINGS

1. string length: In Tcl , you can find the length of a string using the string length command. Example: set my_string "Hello, world!" set length [string length $ my_string ] puts "Length of the string: $length" 2. string reverse: In Tcl , you can reverse a string using the string reverse command. Example: set my_string "Hello, world!" set length [string reverse $ my_string ] puts "Length of the string: $length" STRING COMMAND

3.string compare: In Tcl , you can compare two strings using the string compare command. The string compare command compares two strings lexicographically and returns an integer value indicating their relationship. Example: set string1 "apple" set string2 "banana" # Compare the strings set result[string compare $string1 $string2] if {$result == 0} {     puts "The strings are equal." } elseif {result < 0} {     puts "String 1 comes before String 2." } else {     puts "String 1 comes after String 2." } STRING COMMAND

4.string index: In Tcl , you can extract a single character from a string at a specified index using the string index command. Example: set my_string "Hello, world!" # Get the character at index 1 ( Tcl uses 0-based indexing) set character [string index $ my_string 0] puts "Character at index 0: $character"                    STRING COMMAND

5.string range: In Tcl , you can extract a substring from a string using the string range command. This command allows you to specify a range of characters to extract from the original string. Example: # Example string set my_string "Hello, world!" # Extract a substring from index 1 to 5 set substring [string range $ my_string 1 5] # Print the substring puts "Substring from index 1 to 5: $substring"                    STRING COMMAND

6.string tolower : In Tcl , the string tolower command is used to convert all characters in a string to lowercase. Example: # Example string set my_string "Hello, WORLD!" # Convert the string to lowercase set lc_string [string tolower $ my_string ] # Print the lowercase string puts "Lowercase string: $ lc_string "                    STRING COMMAND

7.string toupper : In Tcl , the string toupper command is used to convert all characters in a string to uppercase. Example: # Example string set my_string "Hello, world!" # Convert the string to uppercase set uppercase_string [string toupper $ my_string ] # Print the uppercase string puts "Uppercase string: $ uppercase_string "                    STRING COMMAND

8.string trim: In Tcl , the string trim command is used to remove whitespace characters (spaces, tabs, and newlines) from the beginning and end of a string. Example: set my_string "   Hello, world!   " # Trim the whitespace from the string set trimmed_string [string trim $ my_string ] # Print the trimmed string puts "Trimmed string: \"$ trimmed_string \""                    STRING COMMAND

9. string trimleft Trim the left side of the begining of the string. set a "  this is tcl class" set b [string trimleft $a] puts $a puts $b set a "   this is tcl class" set b [string trimleft $a " "] puts $a puts $b set a "this is tcl class" set b "this" set c [string trimleft $a $b] puts $a puts $c STRING COMMAND

10. string trim right # trims the right side of the begining of the string. set a "this is tcl class" set b "class" set c [string trimright $a $b] puts $a puts $c 11.string first In Tcl , the string first command is used to find the index of the first occurrence of a substring within a string. set my_string "Hello, world!" set index [string first "world" $ my_string ] puts "Index of 'world': $index" STRING COMMAND

12.string last: In Tcl , the string last command is used to find the index of the last occurrence of a substring within a string. Example: # Example string set my_string "this is a tcl class tcl class tcl " # Find the index of the last occurrence of " tcl " in the string set index [string last " tcl " $ my_string ] # Print the index puts "Index of last ' tcl ': $index" STRING COMMAND

13.string concat : In Tcl , the string concat command concatenates multiple strings together into a single string. It's particularly useful when you want to join several strings into one. Example: set string1 "Hello" set string2 ", " set string3 "world!" set concatenated_string [ concat $string1 $string2 $string3] puts "Concatenated string: $ concatenated_string " STRING COMMAND

14.string equal: In Tcl , the string equal command is used to compare two strings to see if they are equal. It returns a boolean value indicating whether the two strings are equal. Example: set string1 "Hello" set string2 "Hello" if {[string equal $string1 $string2]} {     puts "The strings are equal" } else {     puts "The strings are not equal" } STRING COMMAND

15.string match: In Tcl , the string match command is used to perform pattern matching on strings. It compares a string against a pattern and returns a boolean value indicating whether the string matches the pattern. The pattern can contain special characters to represent wildcards and character ranges. Example: set my_string "hello" set pattern "he*o" if {[string match $pattern $ my_string ]} {     puts "The string matches the pattern" } else {     puts "The string does not match the pattern" } STRING COMMAND

16.string map: In Tcl , the string map command is used to perform multiple string substitutions within a string based on a mapping of key-value pairs. It replaces all occurrences of the keys in the string with their corresponding values. Example: set original_string "The quick brown fox jumps over the lazy dog." set substitution_map {     quick fast     brown red     fox cat     dog rabbit } set modified_string [string map $ substitution_map $ original_string ] puts $ modified_string STRING COMMAND

17. string replace: In Tcl , the string replace command is used to replace a portion of a string with another string. It allows you to specify the start index and the number of characters to replace. Example:     set a "This is TCL class"     puts $a     set b [string replace $a 12 16 "session"]     puts $b STRING COMMAND

18.string repeat: In Tcl , the string repeat command is used to create a new string by repeating another string a specified number of times. Example: set repeated_string [string repeat " abc " 3] puts $ repeated_string STRING COMMAND

19.string format: In Tcl , the string format command is used to create a formatted string based on a format string and a list of values. Example: set name "Vikram" set age 30 set country India set formatted_string [format "Name: %s, Age: %d ,Country:%s" $name $age $country] puts $ formatted_string STRING COMMAND

It is used to match the pattern in the text. In TCL, regular expression is pattern that describes a set of strings, extract the information, and perfrom complex text processing tasks. regexp command is supporting to match the pattern. This command allows to perform various operations such as pattern matching, substitution and splitting on the strings using regular expression. Syntax: [ regexp " search_pattern $content] REGULAR EXPRESSION IN TCL

Example: set a "hello world"   if {[regexp "hello" $a]} {   puts "pattern matches"   } else {   puts "pattern not matches"   } 2) set a "HELLO world"   if {[ regexp - nocase "hello" $a]} {   puts "pattern matches"   } else {   puts "pattern not matches"   } REGULAR EXPRESSION IN TCL

List of all the metacharacters, quantifiers and special sequences that can be used in regular expression in TCL: Meta characters --------------- .(dot)-->matches any single character except newline. set string " abcd " set b [ regexp "^ a..d $" $string] puts $b #output is b=1 REGULAR EXPRESSION IN TCL

set string "I have dog" if {[ regexp " h.e " $string]} { puts "Match found" } else { puts "No Match found" } #output is No Match found |(pipe)-->matches either the pattern on its left or the pattern on its right set string "I have a dog" set b [ regexp " cat|dog " $string] puts $b REGULAR EXPRESSION IN TCL

[...](bracket expression)-->matches any single charcter from the specified set of characters [^...](negated bracket expression)-->matches any single character not in the specified set of characters ()grouping --> groups multiple pattern together. \(escape)-->escapes a meta character,allowing it to be matched as aliteral character. {...}(curly braces)-->matches the enclosed pattern exactly. example:[a-z]{3} REGULAR EXPRESSION IN TCL

Quantifiers ----------- *(asterisk)-->matches zero or more occurrances of the preceding character or subexpression. set string "The quick brown fox jump over the lazy dog"   if {[ regexp "o*" $string} {   puts "match found"   } else {   puts "no match found"   } 2)  set string "The quick brown fox jump over the lazy dog"   set b [ regexp "o*" $string]   puts $b REGULAR EXPRESSION IN TCL

+(plus)-->matches one or more occurances of the preceding character or subexpression set string "The quick brown fox jump over the lazy dog"   set b [ regexp "o+" $string]   puts $b 2) set string "The quick brown fox jump over the lazy dog"   if {[ regexp "o+" $string]} {   puts "match found"   } else {   puts "no match found"   } REGULAR EXPRESSION IN TCL

?(question mark)-->matches zero or one occurances of the preceding character or subexpression. 1) set string "The quick brown fox jump over the lazy dog"   set b [ regexp " ou ?" $string]   puts $b 2) set string "The quick brown fox jump over the lazy dog"   if {[ regexp " ou ?" $string]} {   puts "match found"   } else {   puts "no match found"   } REGULAR EXPRESSION IN TCL

{n}(exact quantifiers)-->matches exactly "n" occurances of the preceding characters or sub expressions {n,}(minimum quantifier)-->matches at least n occurance of the preceding character or sub expressions Example 1: 1) set a {the mentor for funtional_verification2023 is : mohan_kumar_ns }   set b [ regexp - nocase {the mentor for [a-z0-9_]+ is : [a-z]+} $a]   puts $b 2) set a {the mentor for physicaldesign_2023 is : chethan_kumar_ns }   set b [ regexp - nocase {the mentor for ([a-z0-9_]+) is * :* ([a-z_]+)} $a match1 match2 match3]   puts $b   puts "this will print the whole expression :$match1"   puts "this will print the course_name :$match2"   puts "this will print the mentor_name :$match3" REGULAR EXPRESSION IN TCL

Example2: set a { sram :-2500} set b [ regexp - nocase {([a-z]+) :([-0-9]+)} $a m1 m2 m3] puts $b puts "the cell_name is:$m2" puts " the cell_value is:$m3" Example3: lassign $ argv set file [open "text1.txt" r] while {[gets $file line]>=0} { if {$ argv ==[ lindex $line 0]} { set b [ regexp - nocase {([a-z]+) :([-0-9]+)} $line match match2 match3] } } puts "the value of $ argv is : $match3" REGULAR EXPRESSION IN TCL

x --> Exact match [a-z] --> any lowercase from a to z. [A-Z] --> any uppercase from A to Z. . --> any character ^ --> Begining string should match $ --> Ending string should match () --> Add the sequence inside parenthesis to make a RE. x* --> should match 0  or more occurences of the preceding x x+ --> should match 1 or more ocuurences of the preceding x \^ --> backlash sequnece to match special charater . [a-z]* --> should match 0 or more occurence of the preceding x {digit} --> matchs exactly digit ocuurences .             digit occurrences 0 to 9. REGULAR EXPRESSION IN TCL

example:4 1.read the VHDL.txt file 2. go through line by line. 3. write the regular expression to capture all the entity names. 4. 5. write all the entity names into new file(VHDL_enitity.txt) set fh_read [open "VHDL.txt" r] set fh_write [open "VHDL_entity.txt" w] set i while {[gets $ fh_read line] >= 0} { incr i if {[ regexp - nocase { *entity +([a-z_]+) +is} $line match match1]} { puts "the line number of the enity is:$ i and entity name is:$match1" puts $ fh_write "the line number of the enity is:$ i and entity name is:$match1" } } REGULAR EXPRESSION IN TCL
Tags