TCL Handbook

5
GETTING INPUT FROM CONSOLE / BY USER Program 1: Create a text file named args.tcl that contains the following commands. # If no command line arguments are passed perform no actions if {$argc > 0} { # Print out the filename of the script puts "The name of the script is: $argv0" # Print out the count of the arguments passed puts "Total count of arguments passed is: $argc" # Print out a list of the arguments puts "The arguments passed are: $argv" # Using the ist Inde! of argv print a specific argument puts "The first argument passed as linde! $argv 0" } Output : After you have created the file invoke the script with the following command line: % Tclsh8 args!Tcl ONE " # Th$ am$ o& 'h$ scr()' (s: args!Tcl T o'al co*' o& arg*m$'s )ass$+ (s: # Th$ arg*m$'s )ass$+ ar$: ONE " # Th$ &(rs' arg*m$' )ass$+ ,as ONE Programm" : Create a text file named if.tcl  that contains the follow ing commands: # %et the varia&le ! to the argument set ! linde! $argv 0 # Test for condition ' if {$! (( '} { puts ")ondition ' * + ou entered: $!" # Test for condition ' } elseif {$! (( ,} { puts ")ondition , * + ou entered: $!" # If neither condition is met perform the default action } else { puts "$! is not a v alid argument" } Output: Now invoke the script using the following command line: 'clsh8 (&!'cl 1 Co+('(o 1 - Y o* $'$r$+: 1 USE OF E.L COMMN0 Programm # : % s$' comma+ )*'s 23$llo ,orl+24 )*'s 23$llo 5orl+2 % $6al 7comma+ % 3$llo 5orl+ % s$' comma+ $$c o'$)a+4 $$c o'$)a+ % $6al 7comma+ ca'ch!'cl

Transcript of TCL Handbook

8/12/2019 TCL Handbook

http://slidepdf.com/reader/full/tcl-handbook 1/5

GETTING INPUT FROM CONSOLE / BY USER

Program 1:

Create a text file named args.tcl that contains the following commands.

# If no command line arguments are passed perform no actions

if {$argc > 0} {

# Print out the filename of the script

puts "The name of the script is: $argv0"

# Print out the count of the arguments passedputs "Total count of arguments passed is: $argc"

# Print out a list of the arguments

puts "The arguments passed are: $argv"

# Using the ist Inde! of argv print a specific argument

puts "The first argument passed as linde! $argv 0"

}

Output : After you have created the file invoke the script with the following command line:% Tclsh8 args!Tcl ONE " #

Th$ am$ o& 'h$ scr()' (s: args!Tcl

To'al co*' o& arg*m$'s )ass$+ (s: #

Th$ arg*m$'s )ass$+ ar$: ONE " #

Th$ &(rs' arg*m$' )ass$+ ,as ONE

Programm" :

Create a text file named if.tcl that contains the following commands:

# %et the varia&le ! to the argument

set ! linde! $argv 0

# Test for condition '

if {$! (( '} {

puts ")ondition ' * +ou entered: $!"

# Test for condition '

} elseif {$! (( ,} {

puts ")ondition , * +ou entered: $!"

# If neither condition is met perform the default action

} else {

puts "$! is not a valid argument"

}

Output: 

Now invoke the script using the following command line:

'clsh8 (&!'cl 1

Co+('(o 1 - Yo* $'$r$+: 1

USE OF E.L COMMN0

Programm # :

% s$' comma+ )*'s 23$llo ,orl+24)*'s 23$llo 5orl+2

% $6al 7comma+

% 3$llo 5orl+

% s$' comma+ $$c o'$)a+4$$c o'$)a+

% $6al 7comma+ ca'ch!'cl

8/12/2019 TCL Handbook

http://slidepdf.com/reader/full/tcl-handbook 2/5

STRING OPERTION

Bas(c comma+

stringThe string command contains multiple keywords (see the section coveringthe command allowing for manipulation and data gathering functions.

append Appends to a string varia!le.

format "ormat a string in the same manner as C sprint.

rege!p #egular expression matching.

regsu& $erforms su!stitution% !ased on #egular expression matching.

scan $arses a string using conversion specifiers in the same manner as Csscanf.

su&st $erform !ackslash% command% and varia!le su!stitution on a string.

Programm 9 : a))$+;

 set var 0

for {set ! '} {$!-('0}{$!-('0} {incr !} {

append var . $!

}

puts $var

0.'.,./..1.2.3.4.5.'0

Programm 9 : REGE<P;

r$g$) 2=>?-@A1#4;=!=>?-@A1#4;=!=>?-@A1#4;=!=>?-@A1#4;2 7() all &(rs' s$co+ 'h(r+ &o*r'h

)*'s 27all =7&(rs' =7s$co+ =7'h(r+ =7&o*r'h2

O*')*':

1@"!18!1!

1@"

18

1

ow it works… As you can see% the &$ Address has !een split into its individual octet values. 'hat

regexp has done is match the groupings of decimal characters )*+, of a varying length of - to characters /-% 0

delimited !y a 1.1 character. The original &$ address is assigned to the first varia!le (all while the octet values

are assigned to the remaining varia!les (first% second% third% and fourth

Programm 5 : (REGSU E!"#P$E%

8/12/2019 TCL Handbook

http://slidepdf.com/reader/full/tcl-handbook 3/5

set original "one to one to one to"

one to one to one to

regsu& *all {one} $original three ne

&

puts $ne

three to three to three toProgramm ' : ( String length Example%

set input "The end is nigh"The end is nigh

string length $input

'1

programm : (String Compare %

set string' compareset string, linde! $argv 0

set output string compare $string' $string,

puts $output

Joining two lists :

concat {a & c} {' , /} 

Joining list elements

set input {{6ohn 7ar8 9ill} {Tom red %all8}}

{6ohn 7ar8 9ill} {Tom red %all8}

 ;oin $input

6ohn 7ar8 9ill Tom red %all8

Appending list elements

 set input {6ohn 7ar8 9ill}

6ohn 7ar8 9ill

lappend input Tom

6ohn 7ar8 9ill TomAssigning list elements to variables 

lassign {6ohn 7ar8 9ill Tom red} ' , /Tom red

puts "$' $, $/"

6ohn 7ar8 9ill 

Retrieving an element from a list 

8/12/2019 TCL Handbook

http://slidepdf.com/reader/full/tcl-handbook 4/5

 set input {6ohn 7ar8 9ill}

6ohn 7ar8 9ill

linde! $input '

7ar8

Inserting elements into a list

set input {6ohn 7ar8 9ill}

6ohn 7ar8 9ill

set neinput linsert $input ' Tom

6ohn Tom 7ar8 9ill

puts $input

6ohn 7ar8 9ill

puts $neinput

6ohn Tom 7ar8 9ill

Determining the number of elements

llength {6ohn 7ar8 { 9ill Tom }}

/

Getting a list element lrange {6ohn 7ar8 9ill red Tom %all8} 0 '6ohn 7ar8 

Replacing elements

lreplace {a & c d e} ' ' <

a < c d e

earching a list 

lsearch options list pattern 

lsearch =all {6ohn 7ar8 9ill 6ohn 7ar8 9ill} 9ill

, 1

!diting a list set input {6ohn 7ar8 red}6ohn 7ar8 red

lset input ' Tom

6ohn Tom red

orting a list 

lsort =decreasing {a & c d e}e d c & a 

Options )nterpretations

8/12/2019 TCL Handbook

http://slidepdf.com/reader/full/tcl-handbook 5/5

*ascii 2orts using string comparison with 3nicode code*

point collation order.

*dictionar8 2orts using dictionary style comparison.

*integer Converts the l ist elements to integers and use integer

comparison.

*real Converts the list elements to f loat ing*point values

and use f loat ing*point comparison.

*command command 3ses the command provided as a comparison

command.

*increasing 2orts in increasing order.

*decreasing 2orts in decreasing order.

*indices #eturns a lis t of indices in sorted order as opposed to

the values.

*inde! Inde!ist "or this opt ion to !e specif ied each element of the

lis t must !e a proper Tcl su!l ist.

*nocase 2orts using a case*insensitive comparison.

*uniue #eturns only the last set of dup licate

plitting a string into a list

split {6ohn.7ar8.Tom.red.%all8}.

6ohn 7ar8 red Tom %all8