Laurice Menounos
About
-
Posted Questions
Posted Answers
Answer
last modified January 10, 2023
In this part of the Tcl programming tutorial, we cover arrays. We will initiate arrays and read data from them.
A Tcl array is a collection of variables. Each variable may hold any value and the array is indexed by arbitrary values. The key-value pairs are unordered. A Tcl array is an associative array.
Tcl arrays can be created with the set or array set commands.
We create an array called names. The numbers are keys and the names are values of the array.
In this line we set a value Jane to the array key 1. We can later refer to the value by the key.
The array exists command determines whether the provided argument is an array.
We get the size of the array with the array size command.
We access a value from the array by its key.
In the second example, an array is created with the array set command.
We create a days array. It has 7 key-value pairs.
Example output.
Unlike in lists or dictionaries, items in arrays are variables. This means that we can make references to them.
In the script three variables of the days array are referenced with the upvar command.
The mon variable references the variable indexed with 1. The first argument of the upvar command is the uplevel, where #0 means the toplevel. That is, both the days array and the mon variable reside in the same global namespace.
Here we refer to an item of the days array via the mon variable.
Example output.
The array get command returns a list containing pairs of elements of the array.
The example creates an array and prints its key-value pairs with the array get command.
In the following examples, we show how to traverse arrays.
The example creates an array and prints its key-value pairs with the array get command.
The array get command returns a list of key, value elements, which can be iterated with the foreach command.
Here we have the output of the days2.tcl script. Note that the pairs of elements are not ordered.
The following script uses the array names command to traverse an array.
We create a simple nums array and loop through it.
We define a simple array.
The array names returns a list containing the names (the keys) of all of the elements in the array.
We use the keys to get the values.
The previous examples worked with copies of arrays and are therefore less suitable for handling large arrays. Array search facilities are more efficient.
We use array search commands to iterate a simple array.
The array startsearch command references the beginning of the array.
The array anymore command returns 1 if there are any more elements left to be processed in an array search.
The array nextelement command returns the name of the next element in the array.
The array donesearch command terminates an array search and destroys all the state associated with that search.
In the last example of this chapter, we show how to remove elements from the array.
We create a names array. We use the unset command to remove items from the array. We check the size of the array before and after we remove the two items.
The set command is used to create an item in the array.
We use the unset command to remove an element with key 1 from the array.
Answer is posted for the following question.
Answer
: to value (something or someone) too lightly : to fail to properly notice or appreciate (someone or something that should be valued) We often take our freedom for granted. I'm tired of being taken for granted.
Answer is posted for the following question.
Answer
Petrozavodsk is a city located on the shore of Lake Onega in the north-west of the European part of Russia , the capital of the Republic of Karelia It is an
Answer is posted for the following question.
Where is petrozavodsk russia?
Answer
To get XCP-ng installed from PXE, you need: DHCP and TFTP configured servers. Any NFS, FTP or HTTP server (your choice) to host XCP-ng installation files. A PXE-boot compatible network card on your host.
Answer is posted for the following question.
How to install xcp-ng?