Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defining and looping through arrays tcl

Tags:

tcl

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.

like image 284
Micheal Avatar asked Apr 15 '12 19:04

Micheal


People also ask

How do I iterate through a list in Tcl?

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.

How do I set an array variable in Tcl?

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.

How do you create associative array in Tcl?

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.

Which command display each element of array in Tcl?

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.


2 Answers

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)"
}
like image 156
RHSeeger Avatar answered Oct 29 '22 10:10

RHSeeger


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.

like image 22
Edu Avatar answered Oct 29 '22 11:10

Edu