defining and looping through arrays tcl

tcl

Solution

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)"
}

Problem

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.

Original source