|
|
|
|
|
More on variable names when we learn to use variables below....
|
|
|
Example:
set x "Hello World" |
|
set varName value # Assign value to variable varName set varName # Return value in variable varName |
set b 1234 // Associate b with 1234 set b // return 1234 (value associated with b) |
|
set x Hello // assigned Hello to x set x // Obtain the value of variable x |
|
Examples:
set b 56 // Asscoiate b with 56 |
|
That is because the substitution operator ($) assumes that variables names consists only of these characters.
|
set foo-bar 1234 // Associate foo-bar with 1234 puts $foo-bar // Error // Error message: "foo": no such variable // (- character interpreted as minus operation) puts ${foo-bar} // Output: 1234 |
|
variableName ( index ) |
Example:
set A(2) 4567 # A(2) = 4567 puts $A(2) # Print content of A(2) |
|
set A(2) 4567 # A(2) = 4567 puts [array size A] # Output: 1 set A(6) 1111 # A(6) = 1111 puts [array size A] # Output: 2 |
|
int A[] = new int[10]; |
You define the array elements A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8], A[9]
In fact, if A[2] exist, we know that A[0] and A[1] must also exist !
set A(2) 4567 # A(2) = 4567 |
Only element A(2) is created and nothing else.
|
set A(cs455) Cheung # A(cs355) = Cheung set A( cs455 ) Cheung # Error: No spaces allowed in array name !!! |
set dict[red] "Color, associated with fire" set dict[dead] "Opposite of alive" |
Example:
unset A(smith) # unsets A(smith) unset A(jones) # unsets A(jones) |
Example:
unset A # unsets all elements of A |
but associative arrays can simulate them easily:
set A(1,1) 0 set A(1,2) 0 set A(1,3) 0 set A(2,1) 0 set A(2,2) 0 set A(2,3) 0 |
2,2 1,3 2,3 1,1 2,1 1,2 |
So there is no need for nested loops to process multidimensional arrays in TCL