Escolar Documentos
Profissional Documentos
Cultura Documentos
In addition, curly braces can be used to explicitly delimit the variable name (e.g. ${x})
EX : set a 0.16
set b ${a}um
puts $b => 0.16um
Lists :
EX : set myList [list Mark Travnikoff 10644002 May 15 2000]
EX : set var1 4
set var2 PDE
set myList [concat $var1 $var2 ]
puts $myList => 4 PDE
Index :
Range :
Length :
lset :
Insert :
Replace :
Search :
To return the list value instead of the index use the inline option
-all Search for all items that match & return list of matching indices.
-dictionary Compared using dictionary-style comparison. Need to use
-exact or sorted as well.
-exact Do exact string matching
-inline Return the value of the matching element instead of the index.
-integer Compare as integers.
-not Negate the match.
-real Compare as real numbers.
-regexp Use an RE (described in a later module).
-sorted Tell Tcl the list is presorted allows for faster search algorithm.
-start x Specify the start index in the list to begin searching
Sort :
Use the integer or -real switch to sort integers or floating point numbers
Split :
Join :
Note that the indices of arrays do not need to be integers, they can be any string value.
This makes them similar to associative arrays or hashes in other languages
Array exists :
Returns 1 if arrayName is an array variable. Returns 0 if arrayName is a scalar variable, proc, or does not
exist
EX : set array1(0) 0
if {[array exists array1]} {
puts array1 is an array => array1 is an array
} else {
puts array1 is not an array
}
Array Size :
EX : set array1(0) 0
set array1(1) 1
if {[array exists array1]} {
puts array1 contains [array size array1] elements
} => array1 contains 2 elements
Array names :
To convert a list into an associative array use the array command with the set option. Each odd member
of the list (1, 3, 5, etc) will become an index into the associative array, and the list element following
that is the value of that array member
Array get :
To convert an associative array into a list use the array command with the get option. Each odd member
of the list is an index of the original associative array and the even members are the value associated
with that index
Multi Dimension:
Tcl doesnt support multidimensional arrays; however, they can be simulated by concatenating multiple
indices into a single element name.
Note that spaces are significant here, set wwid(Davis, Mike) 10948292 passes three arguments
to the set command (wwid(Davis, Mike), 10948292), which will cause an error
ENV :
The env array is a built-in array whose elements are the environmental variables for the process
puts $env(USER) => mltravni
Command substitution:
Arithmetic :
EX : set var 4
incr var
puts $var => 5
precedence :
+ or - Addition Evaluated last. If there is more than one, the operators are
Subtraction evaluated from right to left.
Strings
A backslash can also be used to continue a long line on a another line
Grouping words with double quotes allows substitutions to occur (i.e. command substitutions,
variable substitutions, backslash substitutions)
Grouping words within curly braces disables substitutions
Field width :
Precision :
Rounding :
String Length :
String range :
string first :
To find the index of the first occurrence of a character use the string command with the first option. -1 is
returned if there is no match
EX : puts first occurrence of \a\ within \$var1 is at index: [string first a $var1]
string last :
To find the index of the last occurrence of a character use the string command with the last option
EX : puts last occurrence of \a\ within \$var1 is at index: [string last a $var1]
string match :
String Compare :
Returns:
-1 if string1 is less than string2
0 if string1 is equal to string2
1 if string1 is greater than string2
String equal :
String lower/upper :
string trim :
Returns string with all occurrences of trimChars removed from both ends
If condition:
EX : if {$var == 1} {
puts The variable is 1.
} else {
puts The variable is not 1.
}
Nested if else :
EX : if {$variable == 2} {
puts The variable is 2.
} elseif {$variable == 1} {
puts The variable is 1.
} elseif {$variable == 0} {
puts The variable is 0.
} else {
puts The variable is not equal to 2, 1 or 0.
}
Switch :
EX : set number 1
switch $number {
0{
puts Match, number is equal to 0
}
1{
puts Match, number is equal to 1
}
default {
puts No matches
}
}
There are four flags that can be used with the switch statement:
-exact The value needs to match exactly one pattern (default behavior).
-glob Uses glob-style pattern matching.
-regexp Uses regular expression pattern matching.
-- No flag (or end of flags). Needed when the value might begin with -.
EX : - exact Example
switch exact -- $name {
PDE { set emp 200; puts The $name group has $emp employees }
CG { set emp 10000; puts The $name group has $emp employees. }
default { puts No match for switch statement.}
}
While :
EX : set number 0
while {$number < 100} {
puts $number is less than 100.
incr number
}
For :
Foreach :
Break :
Immediately exits from the current loop (i.e. the innermost loop).
EX : set x 0
while { $x < 5 } {
incr x
if { $x == 3 } { break }
puts "$x"
}
=> 1
2
Condition :
catch :
Exit :
Files
Open a file :
Closing file :
Read line :
EX : gets $fileId line ;# Read one line from filehandle, store in $line
Write to file :
Command ARGV :
The first command-line argument is stored in the $argv[0] variable, the second in $argv[1]
File Commands :
The file readable command returns 1 if the file is readable by the user, otherwise 0.
The file owned command returns 1 if the file is owned by the user, otherwise 0.
The file exists command returns 1 if the file exists and the user has search access in all the
directories leading to the file, otherwise 0.
The file dirname command will return the directory portion of a path.
If the path contains no slashes, then . is returned.
set filepath /extra1/mltravni/apr_green_go_button.tcl
set dirpath [file dirname $filepath]
puts $dirpath => /extra1/mltravni
The file extension command will return the filename extension
The file rootname command will return the filename without the extension
The file tail command will return the filename without the directory
Returns all of the characters in the filename after the last slash
The file readlink command returns the name of the file (full path) pointed to by a symbolic link.
If the file is not a link or cant be read then an error is returned
The file atime command returns the time the file was last accessed
Returns the number of seconds since 1/1/1970
Generates an error if the file doesn't exist, or the access time cannot be queried
set lastAccess [file atime $file]
The file mtime command returns the time the files was last modified
Returns the number of seconds since 1/1/1970.
set lastMod [file mtime $file]
The file copy command copies a file.
The source and destinations can be directories.
.
Ex: file copy force $file1 $file2 $file3 $destination
file copy .alias .alias_bckup
The file split command splits a path into its pathname components
The file join command joins pathname components into a new pathname
Use join to construct file names.
Syntax: file join name ?name ?
Glob :
Ex : set tcl_files [glob nocomplain *.tcl] ;# Grab all .tcl files in current directory
Regular Expressions
Tcl provides several special escape characters for use in regular expressions.
\d match a digit character (0-9)
\D match a non-digit character
\s match a white-space character
\S match a nonwhite-space character
\w match a letter, digit or underscore
\W match a non-letter/digit/underscore
. match any character
? match zero or one times
* match zero or more times
+ match one or more times
{n} match n times
{n,} match at least n times
{n,m} match between n & m times (inclusive)
^ match the beginning of the string
$ match the end of the string
More Examples
Pattern substitution:
The all option means to replace all occurrences of a pattern, otherwise only the first
occurrence is replaced
The nocase option is used to ignore casing.Basically any uppercase characters in the string are
converted to lower-case during the matching process
Use the -- option if your pattern potentially starts with a dash character.
The -- option basically signals the end of the options and allows patterns to begin with a dash
Procedures