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

TCL Scripting Session-18-30

The document provides an overview of string manipulation, flow control, lists, loops, arrays, procedures, I/O file handling, and regular expression substitution in Tcl. It outlines the syntax and commands associated with each topic, including examples for clarity. The document serves as a guide for understanding basic programming constructs in Tcl.

Uploaded by

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

TCL Scripting Session-18-30

The document provides an overview of string manipulation, flow control, lists, loops, arrays, procedures, I/O file handling, and regular expression substitution in Tcl. It outlines the syntax and commands associated with each topic, including examples for clarity. The document serves as a guide for understanding basic programming constructs in Tcl.

Uploaded by

Mohamed Tarek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Labs

lab1
String Manipulation

• Strings are the basic data type in Tcl, so there are a large number of functions
supported in Tcl to manipulate strings through string command.

• Syntax : string operation string Value arguments

• string's first argument determines what it does (operation).


string's second argument is a string.
there may be additional arguments depending on the operation.

String Manipulation
String Manipulation

• Commands:

There are still many commands

String Manipulation
Flow Control

• allows your script to take different paths based on condition.

• TCL provides some commands to manage the flow of control like:


if……..else and if..elseif..else

• Example:
set sel 3
if {$sel == 1} {
puts "Selection $sel"
} elseif {$sel == 2} {
puts "Selection $sel"
} else {
puts "Invalid selection"
}

Flow Control
Lists

• List is a collection of elements separated by white space, each element with a relative
position within the List. Number of elements in the List defines the size of the List and an
empty List will contain no elements.

• There are a large number of functions supported in Tcl to manipulate lists as list,
lappend, lindex, linsert, llength, Irange, Isearch, concat and join commands.

• Creation of list:-

set mylist “1 2 3 4 5“
set mylist {1 2 3 4 5}
set mylist [list 1 2 3 4 5]
Lists
Loops

• While loop

• For loop

Loops
Loops

• Foreach loop

foreach implements a loop where the loop variable(s) iterates over all
the elements of a list(s).

? Loops
Loop Control Statements

• break

break statement used to exist the loop and the program resumes at the next
statement following the loop.

• continue

continue statement used to skip the current loop iteration and jumps to execute the
next iteration in the loop.

Loop Control Statements


Arrays

• An array is a systematic arrangement of a group of elements using indices. The syntax for the
conventional array is shown below:

set ArrayName(Index) value

• Creation of Array :-
Index Value
set languages(0) Tcl 0 Tcl
set languages(1) "C Language"
1 C Language

There are many commands for


array like array set:
Index Value
Physics 15
array set marks {Physics 15 Math 20} Arrays
Math 20
Procedures

• Like function in programming languages.

proc name {arguments} {


• Syntax :- Commsnd(s);
}

• Example :-

Procedures
I/O Files Handling

• Tcl supports file handling with the help of the built in commands open, read, puts, and close.

• Syntax of opening file :- open fileName accessMode

I/O Files Handling


I/O Files Handling

• Syntax of closing file :- close fileName

• Syntax of closing file :- read fileName

• Syntax of writing file :- puts fileName {string}

• Let’s do some labs………..

I/O Files Handling


RegSub

• The regsub command is used to do string substitution based on pattern match string.

• Syntax :- regsub optionalSwitches patterns searchString subString varName

• Switches :-
Switch Description
-nocase Used to ignore case
-all Used to substitute all matched strings

• Let’s do some labs ………..

RegSub

You might also like