C-Like Array in TCL -


i'm porting program c tcl, , i'm trying implement data structure similar array in c. 2 main things need are

  • be ordered
  • allow insertion index
  • return array procedure

i know size of array before run time, , size should not change throughout program (so static). there data structures fit bill?

i'm using tcl 8.6 if matters

edit: need able return data structure function.

the corresponding data structure list. meets requirements. if want have fixed size, "pre-allocate" this:

set data [lrepeat 8 {}] 

which creates 8 empty compartments.

it ordered, can access every element index (0-based), , can pass value procedures/functions , return it. can traverse e.g. foreach , for, , there lot of list manipulating commands.


while list tcl data container corresponds closely c array, 1 can use array or dict simulate fixed-size, direct-access, ordered container.

# allocation set listdata [lrepeat 8 {}] array set arraydata {0 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {}} set dictdata [dict create 0 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {}] # or set dictdata {0 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {}}  # access (write, read) lset listdata 5 96 lindex $listdata 5 ;# -> 96 set arraydata(5) 96 set arraydata(5) ;# -> 96 dict set dictdata 5 96 dict $dictdata 5 ;# -> 96  # return procedure # (not possible arraydata, arrays shared using either # scope manipulation or namespaces) return $listdata return $dictdata  # traversal {set 0} {$i < 8} {incr i} {puts [lindex $listdata $i]} # or foreach elem $listdata {puts $elem} {set 0} {$i < 8} {incr i} {puts [lindex $arraydata($i)]} dict {idx val} $dictdata {puts $val} 

documentation: array, dict, for, foreach, incr, lindex, lrepeat, lset, puts, return, set


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -