nandini
nandini

Reputation: 11

Is that possible to create a List of list in tcl

How to create a list of list if i m having a list "outer_list" pointing to a "inner_list" contains "inner_list " contains some keys & values like keys { aaa bbb ccc jjj kkk lll ooo } and values {22 34 56 78 90 67 88}

Upvotes: 1

Views: 223

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137557

Lists are plain old values in Tcl, and you can put them inside each other as much as you want. The list command can be very helpful here.

set inner_list1 {aaa bbb ccc jjj kkk lll ooo}
set inner_list2 {22 34 56 78 90 67 88}
set outer_list [list keys $inner_list1 values $inner_list2]

If you were going to instead zip things together, you might do that with foreach and lappend:

set outer_list {}
foreach key {aaa bbb ccc jjj kkk lll ooo} value {22 34 56 78 90 67 88} {
    lappend outer_list [list $key $value]
}

(If you're really doing key/value pairs, consider using a dictionary instead.)
There are so many different ways to build a nested list structure; the best way to do it will depend ever so much on exactly what you are trying to construct. About the only restriction is that you can't create recursive structures with lists (or dictionaries) — Tcl copies things if you try — yet you can still build very large data structures if you want…

Upvotes: 1

Related Questions