Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TCL array question using foreach command...

Status
Not open for further replies.

cliffcolon

Programmer
Sep 14, 2006
1
US
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(n) )? 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
 
Sounds like a generalized number-mnemonic routine like, say for telephone numbers or cipher lock codes. I tried to come up with a way some time ago but was unsuccessful. However, it might make a difference depending on your real goal: is it just to print the patterns? You might consider building a multidimensional array from the "array of lists" by initializing to the number of elements in each list. Then you could compute the number of times, say, element 1 of list 1 will appear in column 1, etc. This was just too much work for a mnemonic generator so I didn't pursue it.

_________________
Bob Rashkin
 
hi cliffcolon,

you might want to try this:
Code:
#===============================================================================
proc dumpArray {arr keys {prefix ""}} {
#===============================================================================
    upvar $arr array
    if {[llength $keys] == 0} {
        puts $prefix
        return
    }
    set key     [lindex $keys 0]
    set keys    [lrange $keys 1 end]
    foreach v $array($key) {
        dumpArray array $keys "$prefix $v"
    }
}

#
# Example 1
#
set TheArray(1) [list 1 2 3]
set TheArray(2) [list 4 5 6]
set TheArray(3) [list 7 8 9]

dumpArray TheArray [array names TheArray]

#
# Example 2
#

set TheArray2(1) [list 1 2 3]
set TheArray2(2) [list 4 5 6 7 8 9]

dumpArray TheArray2 [array names TheArray2]

Greetings,
Tobi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top