0

It may be a trivial question; but I am facing problem to covert a list to array inside a tcl script.

set mylist { one two three four five }

I need to convert this to array myArr. One more doubt, Can we use list elements as separate, like I want to use second element of mylist i.e. mylist(1)?

1
  • "arrays" in tcl are what are actually (unordered) associative arrays (what is called "dictionaries" or "hashes" in other languages). There is no point in converting a list to an array, as you can always access any element of a list with lindex: puts [lindex $mylist 1] for two in your example.
    – user313992
    Jun 17, 2020 at 18:29

1 Answer 1

1

At it simplist, you can go through the list and increment an index i to set that array's element myArr($i):

set i 0
foreach n $mylist {
 set myArr($i) $n
 incr i
}

You can then refer to $myArr(1), but for the list you need [lindex $mylist 1].

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .