Go to: https://www.cnblogs.com/huguodong/p/5890146.html
1. Introduction
Arrays in Tcl are a bit different from arrays in other high-level languages: the index, or key, of a Tcl array element can be any string, and there is no concept of a multidimensional array in itself. The access speed of arrays has advantages over lists. Arrays are internally stored using hash tables, and the access cost of each element is almost the same. The time spent accessing data in lists is not proportional to its length.
Second, the definition and format of the array
The array index is specified by parentheses ( ), and the format of the variable name of each array element is "array name (index value)". array
Elements are defined and assigned using the set command:
[ Syntax ] : set arrName( index ) value
You can also use the array command to define an array:
[语法]:array set arrName { index1 value1 index2 value2 ...}
This command defines its elements and element values while defining an array. It should be noted that the element index (index-n) and the element value (valun-n) must be entered in pairs, otherwise an error will occur. An empty array can be defined with the command array set arrName "". Use the normal variable value acquisition method - the replacement operation to get the array element value:
[ Syntax ] : set val $arrName( index)
Array element indices also support substitution operations, including variable and command substitution, such as:
[ Syntax ] : set val $arrName($index)
set val $arrName([expr $index + 1])
Unlike other high-level languages such as C, the value of Tcl array index must be an integer. Tcl allows index values to be strings of all legal characters including numeric characters.
array set arr1 "" ;# defines an empty array set array01(5) "Hello World" result => Hello World puts $array01(5) result => Hello World set array01(Hello) World result => World puts $array01(Hello) result => World array names array01 ;#array names command displays array element names result => Hello 5 array set arr2 {1 a 2 b 3 c 4 d} array names arr2 result => 4 1 2 3 parray arr2 ;# output the entire contents of the array result => arr2(1) = a arr2(2) = b arr2(3) = c arr2(4) = d
3. Multidimensional array
At some point, you may need something like the C language:
int arr[2][2] arr[0][0] = 100
to define a multidimensional array to process the data. Tcl does not directly support the format of this array, the user can define
Define so-called multidimensional arrays, such as:
set arr(0,0) 100 set arr(0,1) 200 parray arr result => arr(0,0) = 100 arr(0,1) = 200
Due to the flexibility of Tcl array indexing, be careful when using it, otherwise you may not get the expected results, such as forgetting the above index
The quoted comma becomes:
set arr(00) 100 parray arr result => arr(0,0) = 100
arr(0,1) = 200
Fourth, the array operation instructions
command format |
illustrate |
array exists arr |
To determine whether arr is an array variable, it returns 1 |
array get arr ?pattern? |
Returns a list containing alternating indices, element values. pattern selects the matching index. If pattern is not specified, returns all element indices and values. |
array names arr ?pattern? |
return index |
array set arr list |
initialize the array |
array size arr |
array size |
array startsearch arr |
Returns the search token used for arr to search |
array nextelement arr index |
Returns the next element value, or an empty string if it is already at the end |
array donesearch arr index |
end the search with the index flag |
parray arr |
Print out all element variable names and element values of arr |
1.array get command
The array get command extracts array index, element value pairs and organizes these value pairs into a list. The array set command converts a list (data in pairs) into an array. example
array set arr [list a AAA b BBB c CCC d DDD] array size arr ;#Number of array elements result => 4 parray arr Result => arr(a) = AAA arr(b) = BBB arr(c) = CCC arr(d) = DDD set l1 [array get arr] Result => d DDD a AAA b BBB c CCC
2.array names command
array names Returns a list of all element index names matching the pattern pattern. The patterns pattern and string match have the same pattern format. If pattern is not specified, returns a list of all array element index names.
array set a [list "School,BUPT" "BUPT" "School,NJU" "NJU" "School,NJUA" "NJUA"] % parray a 结果=> a(School,BUPT) = BUPT a(School,NJU) = NJU a(School,NJUA) = NJUA array names a "School,*" Result=>School,NJU School,NJUA School,BUPT array names a "School,N*" Result=>School,NJU School,NJUA array names a Result=>School,NJU School,NJUA School,BUPT