#! /home/ncl/mmme1/nmansys/bin/wish #the above line makes this file executable so I don't bother with wish #you might be best to delete it if you want to source from #within wish if [winfo exists .darea] {destroy .darea} #if the script has been run before the variables may already exist #this line gets rid of them wm title . "Demonstration of Matrix C.Hicks 02/2001" wm minsize . 500 300 frame .darea -width 25c -height 20c #defing the size of the matrix set rows 6 set columns 4 #a nested loop follows which produces the matrix first by column then #by row #notice how indents are used in the code to make the structure of the #for loops obvious #go through the columns for {set i 1} {$i < [expr $columns +1]} {incr i +1} { #first we need to create a frame for each column frame .darea.frame$i #we next go through the rows for each column for {set j 1} {$j < [expr $rows +1]} {incr j +1} { #we use a multideminesional array to represent the matrix. set matrix($i,$j) "i = $i j = $j" # just for info we write out the contents of the matrix so you can see #the order that things happen puts $matrix($i,$j) #we create a text entry for the cell $i$j entry .e$i$j -width 20 -relief sunken -bd 2 -textvariable matrix($i,$j) #just as a demo we also produce a label for each cell label .label$i$j -text "label$i$j:" } } #notice how I lined up the closing braces with the start of the for loop #we first pack the frames for {set i 1} {$i < [expr $columns +1]} {incr i} { pack .darea.frame$i -side left } #we next pack the entries and the labels column by column for {set i 1} {$i < [expr $columns +1]} {incr i} { for {set j 1} {$j < [expr $rows +1]} {incr j +1} { pack .label$i$j .e$i$j -side top -in .darea.frame$i -padx 1m -pady 2m } } pack .darea #if you want to manipulate the matrix, the way to do this is #to create another multidimensional array say matrix1 and then #write some code to copy data from matrix to reflect any changes #you want to make. You then display the new multi-dimensional array