Reputation: 90
I have a procedure that returns a list of lists. I can't figure out how to access the inner lists without splitting them first. I'm sure there must be a cleaner way.
For example, this:
proc return_l_of_l {} {
set x {a b c}
set y {d e f}
return [list [list $x] [list $y]]
}
set l [return_l_of_l]
set x_list [lindex $l 0]
set y_list [lindex $l 1]
foreach x $x_list { puts $x }
foreach y $y_list { puts $y }
outputs:
a b c
d e f
not:
a
b
c
d
e
f
Upvotes: 0
Views: 1767
Reputation: 55453
proc return_l_of_l {} {
set x {a b c}
set y {d e f}
return [list [list $x] [list $y]]
}
returns a list of two lists each consisting of a single element — a b c
and d e f
. That's because {a b c}
and {d e f}
are already constructs which can be interpreted as lists.
Supposedly you'd just need
return [list $x $y]
Upvotes: 0
Reputation: 137567
The problem is this line:
return [list [list $x] [list $y]]
Since x
and y
already hold lists, it makes a list of lists of lists. You should instead do:
return [list $x $y]
or possibly:
return [list [list {*}$x] [list {*}$y]]
Upvotes: 3