Defining and scrolling tcl arrays

I need help defining arrays, as well as their mapping and cyclization in TCL.

This is how I will make 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 need the following code?

 set size 0 set date[$size] $pre_event_date #After performing some manipulation for {set i 0} { $i <=$size } {incr i} { puts "$date[$i]"; } 

You can also define the value of $ date as an array. Some like:

 set date array(); 

So, I edited my code, tried a simple test using the implementation of the RSeeger array:

 set date(0) 35 set date(1) 40 foreach key [array names date]{ puts "${key}=$date($key)" } 

the above does not return anything, maybe there is some error. I also tried: puts $ date ($ key) without quotes, but that doesn't work either.

+6
source share
3 answers

If you want to index things by number (which means your code), use list . It is similar 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 objects line by line (or have a sparse list), you can use array , which is a hash map 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)" } 
+8
source

In Tcl, the concept of an array is different from many other programming languages, and the fact that Tcl calls an array is often called a hash map or associative array elsewhere. Array indices are not limited to integers, but can be any legal strings. In most cases, I use lists (or lists of lists) instead of arrays to process the data. You can use the foreach command to loop through an entire list or array.

 foreach {index content} [array get date] { put $index: $content } 

You do not need to initialize the array before setting its values, just start adding it. The individual elements of the array refer to

  $array($key) or $array("abc") 

There are no multidimensional arrays in Tcl, but they can be modeled with consistent key names, for example

 set a(1,1) 0 set a(1,2) 1 ... 

Other than that, I’ll just show you the mostly excellent Tcl wiki and the array page and the man page array for syntax problems, since I don’t see the point repeating most of their contents here.

+5
source

TCL arrays are closer to what Python calls a dict, and a hash in Perl. Thus, it helps to think of the index as a key value, not an integer number of indices:

 set groceries(fruit) "banana" set groceries(1) "banana" 

You can use the array function to do useful things, for example generate a list of all keys, and you can iterate over this list if you need to. For example, you can generate a list of keys and then use llength to get the size of the array.

This works best if you need a unique index to find them. If you only need an ordered list, then you better use the actual functions of the list, such as lappend .

+3
source

Source: https://habr.com/ru/post/913266/


All Articles