cliffcolon
Programmer
This is my first post on this site. I am having a problem creating a function to dynamically print a TCL array of lists and I was hoping someone might be able to get me on the right direction. Here is the scenario:
proc DoSomething {Array} {
array set TheArray $Array
foreach a $TheArray(1) {
foreach b $TheArray(2) {
foreach c $TheArray(3) {
puts "$a $b $c"
}
}
}
}
variable TheArray
set TheArray(1) [list 1 2 3]
set TheArray(2) [list 4 5 6]
set TheArray(3) [list 7 8 9]
DoSomething [array get TheArray]
This will print the following:
1 4 7
1 4 8
1 4 9
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
2 4 7
2 4 8
2 4 9
2 5 7
2 5 8
2 5 9
2 6 7
2 6 8
2 6 9
3 4 7
3 4 8
3 4 9
3 5 7
3 5 8
3 5 9
3 6 7
3 6 8
3 6 9
This is the easy part. What if I wanted to create a function that didn't know how many elements were in the array (TheArray )? Is there a cool trick for handling a scenario like this? In other words, I don't know the total amount of elements in the array, therefore I don't know how many "foreach" statements to write.
For example, here are some other lists I might want to send this function:
variable TheArray2
set TheArray2(1) [list 1 2 3]
set TheArray2(2) [list 4 5 6 7 8 9]
variable TheArray3
set TheArray3(1) [list 1 2 3]
set TheArray3(2) [list 4 5 6]
set TheArray3(3) [list 7 8 9]
set TheArray3(4) [list 10 11 12 13]
set TheArray3(5) [list 14 15]
Does anyone know a cool way to do this? Any information would be greatly appreciated.
Thanks!
Cliff
proc DoSomething {Array} {
array set TheArray $Array
foreach a $TheArray(1) {
foreach b $TheArray(2) {
foreach c $TheArray(3) {
puts "$a $b $c"
}
}
}
}
variable TheArray
set TheArray(1) [list 1 2 3]
set TheArray(2) [list 4 5 6]
set TheArray(3) [list 7 8 9]
DoSomething [array get TheArray]
This will print the following:
1 4 7
1 4 8
1 4 9
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
2 4 7
2 4 8
2 4 9
2 5 7
2 5 8
2 5 9
2 6 7
2 6 8
2 6 9
3 4 7
3 4 8
3 4 9
3 5 7
3 5 8
3 5 9
3 6 7
3 6 8
3 6 9
This is the easy part. What if I wanted to create a function that didn't know how many elements were in the array (TheArray )? Is there a cool trick for handling a scenario like this? In other words, I don't know the total amount of elements in the array, therefore I don't know how many "foreach" statements to write.
For example, here are some other lists I might want to send this function:
variable TheArray2
set TheArray2(1) [list 1 2 3]
set TheArray2(2) [list 4 5 6 7 8 9]
variable TheArray3
set TheArray3(1) [list 1 2 3]
set TheArray3(2) [list 4 5 6]
set TheArray3(3) [list 7 8 9]
set TheArray3(4) [list 10 11 12 13]
set TheArray3(5) [list 14 15]
Does anyone know a cool way to do this? Any information would be greatly appreciated.
Thanks!
Cliff