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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tcl 2D array

Status
Not open for further replies.

pradyot

Programmer
Nov 10, 2002
1
0
0
US
I was wondering if someone could tell me how a 2D array can be declared and used in Tcl.I have seen seen an awnser on how to store 2values agagainst 1 index.I need to store 1 value against an (x,y0 index.
thanks
Pradyot
 
The usual method for emulating two-d arrays is something like this:
Code:
array set myarray {}
for {set x 0 ; set c [expr $x + 1]} {$x <100} {incr x  ; incr c} {
                set myarray($x,$c)      [expr int(1 + rand() * $c)]
}
[\code]

Which creates an indiced array that can be handled as
a two dimensional data structure.
Too fully emulate a C type 2d array's indexing scheme is  
simple too.
[code]
for {set x 0} {$x < 100} {incr x} {
              for {set c 0} {$c < 10} {incr c} {
                  set myarray($x,$c) [expr int(1 + rand() * $x + $c)]
              }
}

HTH
 
Here is a method to access the resulting array as well.
Code:
proc retrieveInd {arrname ind {indopt &quot;*&quot;}} {
           upvar #0 $arrname local
             if {&quot;$indopt&quot; == &quot;*&quot;} {
                 puts &quot;Printing all indexes for array index $ind&quot;
                 foreach a [array name local $ind,$indopt] {
                       regexp &quot;(\[0-9\]+),(\[0-9\]+)&quot; $a a1 a2 a3
                       puts &quot;Index $a2, element $a3 = $local($a)&quot;
                 }
             } else {
                 foreach a [array name local $ind,$indopt] {
                       regexp &quot;(\[0-9\]+),(\[0-9\]+)&quot; $a a1 a2 a3
                       puts &quot;$a2,$a3 =  $local($a)&quot;
                 }
             }
return 0
}
[\code]

Sample run
retrieveInd myarray 12 3
12,3 =  10
0
or:
retrieveInd myarray 15
Printing all indexes for array index 15
Index 15, element 0 = 3
Index 15, element 1 = 11
Index 15, element 2 = 6
Index 15, element 3 = 4
Index 15, element 4 = 17
Index 15, element 5 = 12
Index 15, element 6 = 16
Index 15, element 7 = 20
Index 15, element 8 = 21
Index 15, element 9 = 23
0

HTH
 
marsd gave you some fine examples of emulating multi-dimensional arrays in Tcl. As you can see, there is no problem in creating and using things that look like multi-dimensional arrays. But keep in mind that Tcl is really storing this information in the equivalent of a single-dimensional array. When you do something like:

[tt]set color(1,2) red[/tt]

this creates an element in the color array whose key (index) is the string &quot;1,2&quot; and whose value is &quot;red&quot;. Similarly, when you do the following:

Code:
set x 5
set y 7
set color($x,$y) blue

Tcl performs variable substitution to get the values of x and y, concatenates the results, and comes up with a key of &quot;5,7&quot;.

The only thing you need to watch out for is that since Tcl is really using these strings as keys, rather than real numerical values, if you were to accidentally get an extra space or other value in there, you might get unexpected results. For example:

Code:
set x &quot;1&quot;
set y &quot; 2&quot;
puts &quot;$color($x,$y)&quot;

This last command will produce an error in this case, because the key that Tcl's looking for is &quot;1, 2&quot;, which doesn't match the key of the element we created, which was &quot;1,2&quot;. - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top