String and Array in TCL


Tool Command Language(TCL)- String and Array



String Manipulation


A string is an arbitrary collection of characters it can be used to store any kind of text information. Strings can be searched, compared, formatted, scanned, converted, and trimmed.


String Commands



string length Command


Syntax:

string length "your String here"

Description:

Returns the length of the string

Example:

string length “A test string”

Output: 13

Results:

Returns the number of characters in the string, that is 13

string index Command


Syntax:

string index string charIndex

Description:
Returns the charIndex character of string, or an empty string if it does not exist

Example:

string index “Hello World” 4

Output: o

Results:

Returns the 4th character in the string, “o”

string range Command


Syntax:

string range string first last

Description:

Returns the substring of string that lies between the first and last indices

Example:

string range “A test string” 7 end

Output: string

Results:

The substring from character 7 to the end is returned

string first Command


Syntax:

string first string1 string2

Description:

Returns the first position where string1 is found in string2, or -1 if it is not found

Example:

string first “!” “Help!Me!”

Output: 4

Results:

Finds the first ! at index 4 of the second string

string last Command


Syntax:

string last string1 string2

Description:

Returns the last position where string1 is found in string2, or -1 if it is not found

Example:

string last “!” “Help!Me!”

Output: 7

Results:

Finds the last ! at index 7 of the second string

string compare Command


Syntax:

string compare string1 string2

Description:

Compares the string and returns -1, 0, or 1 if string1 is less than, equal to, or greater than string2

Example:

string compare “First” “Second”

Output: -1

Results:

“First” is less than “Second”, so a -1 is returned

string match Command


Syntax:

string match pattern string

Description:

Returns 1 if pattern matches string and 0 if it doesn’t

Example:

string match “*test*” “A test string”

Output: 1

Results:

The pattern matches the string, so a 1 is returned

string tolower Command


Syntax:

string tolower string

Description:

Return a lowercase version of string

Example:

string tolower “A Test String”

Output: a test string

Results:

All the uppercase characters are converted to lowercase

string toupper Command


Syntax:

string toupper string

Description:

Return a uppercase version of string

Example:

string toupper “A Test String”

Output: A TEST STRING

Results:

All the lowercase characters are converted to uppercase

string trim Command


Syntax:

string trim string ?chars?

Description:

Trims characters found in chars from each end of the string, default is white space

Example:

string trim “ A test string !!!!” “ !”

Output: A test string

Results:

Removes all spaces and !’s from each end of the string

string trimleft Command


Syntax:

string trimleft string ?chars?

Description:

Trims characters found in chars from the left end of the string, default is white space

Example:

string trimleft “ A test string !!!!” “ !”

Output: A test string !!!!

Results:

Removes all spaces and !’s from the left end of the string

string trimright Command


Syntax:

string trimright string ?chars?

Description:

Trims characters found in chars from the right end of the string, default is white space

Example:

string trimright “ A test string !!!!” “ !”

Output:  A test string

Results:

Removes all spaces and !’s from the right end of the string

regexp Command

Syntax:

regexp exp string

Description:

Returns 1 if regular expression exp matches all or part of string, otherwise it returns 0


regexp Command


Example:

regexp "does|matchquot; "Does this match"

Output: 1

Results:

The regular expression matches the string, so a 1 is returned. The expression translates to: “Does the searched string contain the word ‘does’ OR is ‘match’ the last word of the string?”

regsub Command


Syntax:

regsub exp string substitute varName

Description:

Copies string to varName.

While copying, if a match for exp is found, it is replaced with substitute.

The number of substitutions is returned.



regsub Command


Example:

regsub "matchquot; "match test - Does this match" "**replaced**" sStr ==>1

puts $sStr

Output:  match test - Does this **replaced**

Results:

The portion of the string that matched the regular expression is replaced with another string, and the resulting string is placed in the variable sStr. The number of substitutions is returned.

format Command


Syntax:

format formatString ?value value …?

Description:

Returns a string similar to formatString, except that % sequences have been replaced by the given values.

Example:

format "The %s number %d is %x in %s" \ decimal 23 23 hex

Output: The decimal number 23 is 17 in hex

Results:

A string is printed using the format defined


scan Command


Syntax:

scan string format varName ?varName …?

Description:

Parses the string according to the format and assigns values to varName(s)

Example:

scan "Decimal 23 = 17" "%s %d = %x" a b c

Output: 3

Results:

Variable a=Decimal, b=23 and c=23. Returns number of scanned items (3).

Lists


A list is an ordered set of items containing any string value separated by white space.It can be nested to any level. It is useful to process information returned from MQL commands.


list Command


Syntax:

list ?value value …?

Description:

Creates a list from the value arguments and returns it

Example:

set lComps [list Memory Mouse “System Box”]

Output: Memory Mouse {System Box}

Results:

A list is created from the arguments and stored in the lComps variable

concat Command


Syntax:

concat ?list list …?

Description:

Concatenates multiple lists and returns the resulting list

Example:

set lComps [concat $lComps [list Monitor Keyboard]]

Output: Memory Mouse {System Box} Monitor Keyboard


Results:

The lComps list is concatenated with the list of Monitor and Keyboard to form a new list of Components

lappend Command


Syntax:

lappend varName value ?value …?

Description:

Appends all the values as a list to the list varName. **Creates varname if it does not exist.

Example:

lappend lComps Monitor

Output: Memory Mouse {System Box} Monitor


Results:

The Monitor is appended as a new item to lComps.

lindex Command


Syntax:

lindex list index

Description:

Returns the index item from the list varName (0 is the first item)

Example:

lindex $lComps 2

Output: System Box


Results:

The 3rd item (index 2) is returned from the lComps list

llength Command


Syntax:

llength list

Description:

Returns the number of items in the list

Example:

llength $lComps

Output: 5

Results:

Returns the number of items in the lComps list

lrange Command


Syntax:

lrange list first last

Description:

Returns the list of items first through last of the list

Example:

lrange $lComps 0 2

Output: Memory Mouse {System Box}


Results:

Items 0 through 2 of lComps are returned

linsert Command


Syntax:

linsert list index value ?value …?

Description:

Returns a list consisting of the original list with all the values inserted as list items before the indexed item

Example:

linsert $lComps 2 RAM

Output: Memory Mouse RAM {System Box} Monitor Keyboard

Results:

The value RAM is inserted before item 2 in the lComps list

lreplace Command


Syntax:

lreplace list first last ?value value …?

Description:

Returns the list with items first through last of list replaced with values

Example:

lreplace $lComps 3 3

Output: Memory Mouse RAM Monitor Keyboard

Results:

Item 3 of list lComps is replaced with nothing

lsearch Command


Syntax:

lsearch ?-glob? ?-exact? ?-regexp? list pattern

Description:

Returns the index of the first item in list that matches pattern according to the specified technique (-1 if there are no matches)

lsearch Command


Example:

lsearch -glob $lComps *r*

Output: 0


Results:

Returns the index of the first item in list lComps that matches *r*, which is Memory, item 0

Memory Mouse RAM Monitor Keyboard


lsort Command


Syntax:

lsort ?options? list

Description:

Sorts the list based on the specified options and returns it. Options include -ascii, -integer, -real, -dictionary, -increasing, -decreasing, -index x, -command cmd .

Example:

lsort -ascii $lComps

Output: Keyboard Memory Monitor Mouse RAM

Results:

The lComps list is sorted and returned

split Command


Syntax:

split string ?splitChars?

Description:

Splits the string at each instance of splitChars and returns a list

Example:

split [mql list group] \n

Output: {HCC Corp} Sales Manufacturing

Results:

The string is split at each instance of the newline character and the resulting list is returned

join Command


Syntax:

join list ?joinString?

Description:

Concatenates all the list items placing joinString between them and returns the resulting string

Example:

join $lGroups “|”

Output: HCC Corp|Sales|Manufacturing

Results:

The list is concatenated placing | between each list item and the resulting string is returned




Arrays in TCL


Tcl arrays consist of a collection of name and value pairs, which is often called an associative array (a name associated with a value)

arrayvariable(index) --------> Value

Array variables can be used anywhere simple variables are used.It indices can be any string but space characters should be avoided.

Array Variables


Example:

set aPerson(firstname) Tom

set aPerson(lastname) Jones

Results:

The array aPerson is created. The “firstname” element is created in the aPerson array and given the value “Tom”. The “lastname” element is created in the aPerson array and given the value “Jones”.

Array variable values are accessed the same way as simple variables using $ substitution


puts $aPerson(firstname)

Output: Tom

Array Commands


Commands that are used to access and manipulate arrays in Tcl:

array exists Command


Syntax:

array exists arrayName

Description:

Checks if an array exists

Example:

array exists aPerson

Output: 1

Results:

Returns 1 indicating the array variable exists

array size Command


Syntax:

array size arrayName

Description:

Returns the number of name-value pairs in the array

Example:

array size aPerson

Output: 2

Results:

The number of name-value pairs in the aPerson array is returned

array names Command


Syntax:

array names arrayName

Description:

Returns each name in the array

Example:

array names aPerson

Output: firstname lastname

Results:

The names in the array aPerson are returned

array set Command


Syntax:

array set arrayName list

Description:

Creates an array named arrayName from the elements in the list

Example:

array set aPerson {name Joe job Boss}

Results:

Creates array aPerson and associates name with Joe and job with Boss

array get Command


Syntax:

array get arrayName

Description:

Returns each name-value pair in the array as list

Example:

array get aPerson

Output: name Joe job Boss

Results:

The name-value pairs in the array aPerson are returned

env Array


Tcl maintains an internal array named env

Allows access to the operating system environment variables

Examples:

array get env

Output: HOME C:\\COMSPEC{C:\WINNT\system32\...


puts $env(USERNAME)

Output: smith


tcl_platform Array


Tcl maintains an array with client specific information


Example:

array get tcl_platform

Output: byteOrder littleEndian osVersion 4.0 machine intel platform windows os {Windows NT}

info Command


Displays information about Tcl commands, variables, procedures, machine hostname, etc.

Syntax:

info keyword arg…

Examples:

info vars s*

info exists sPart

info procs

0 comments:

Post a Comment

 

Google+ Followers

Follow by Email

Popular Posts

© 2012 Enovia PLM Tutorial . Designed by Nitin Maheta, Powered by Blogger