I need some help in defining arrays and displaying and looping thrrough them in TCL.
Here is how I would do them in php.
$date =array();
$size=0;
$date[$size] =$pre_event_date;
/* After doing some manpulation and calculations with $size */
for($i=0;$i<=$size;$i++){
echo $date[$i];
}
I would like to do the same with tcl.Is the following code appropriate?
set size 0
set date[$size] $pre_event_date
#After performing some manipulation
for {set i 0} { $i <=$size } {incr i} {
puts "$date[$i]";
}
Also can I define set $date as an array. Some like like:
set date array();
So i edited my code tried a simple test using RSeeger's array implementation:
set date(0) 35
set date(1) 40
foreach key [array names date]{
puts "${key}=$date($key)"
}
the above doesnt return anything there is probably some error. I also tried: puts $date($key) without quotes but that doesnt work either.
Tcl Built-In CommandsThe foreach command implements a loop where the loop variable(s) take on values from one or more lists. In the simplest case there is one loop variable, varname, and one list, list, that is a list of values to assign to varname. The body argument is a Tcl script.
Creating arraysTcl 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.
Associative Arrays In Tcl, all arrays by nature are associative. Arrays are stored and retrieved without any specific order. Associative arrays have an index that is not necessarily a number, and can be sparsely populated. A simple example for associative array with non-number indices is shown below.
array startsearch arrayName This command initializes an element-by-element search through the array given by arrayName, such that invocations of the array nextelement command will return the names of the individual elements in the array.
If you're looking to index things by number (which your code implies), use a list
. It is analogous to an array in C.
set mylist {}
lappend mylist a
lappend mylist b
lappend mylist c
lappend mylist d
foreach elem $mylist {
puts $elem
}
// or if you really want to use for
for {set i 0} {$i < [length $mylist]} {incr i} {
puts "${i}=[lindex $mylist $i]"
}
If you want to index things by string (or have a sparse list), you can use an array
, which is a hashmap of key->value.
set myarr(chicken) animal
set myarr(cows) animal
set myarr(rock) mineral
set myarr(pea) vegetable
foreach key [array names myarr] {
puts "${key}=$myarr($key)"
}
In Tcl, array concept differs from many other programming languages and what Tcl calls an array is often called hash map or associative array elsewhere. Array indices are not limited to integers but can be any legal strings. Most of the time I find myself using lists (or lists of lists) instead of arrays for data manipulation. For looping through a whole list or an array you can use command foreach.
foreach {index content} [array get date] {
put $index: $content
}
You don't have to initialize the array before setting it's values, just start adding the members. Individual array members are referenced as
$array($key) or $array("abc")
There are no multi-dimensional arrays in Tcl, but they can be simulated by having consistent key names, for example
set a(1,1) 0
set a(1,2) 1
...
Other than that, I would just point you to mostly excellent Tcl wiki and it's array page and array man page for syntax issues as I don't see point repeating most of their content here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With