Jan 3, 2003 #1 jlynch1 Technical User Dec 20, 2001 100 IE How can I create a 2d array in tcl. I want to store 2 values for every index. eg index 0 contains values 1 & .203
How can I create a 2d array in tcl. I want to store 2 values for every index. eg index 0 contains values 1 & .203
Jan 3, 2003 #2 marsd IS-IT--Management Apr 25, 2001 2,218 US Sample procedures: proc D2 {} { set x 0 array set arr {} while {$x < 200} { set arr($x,"2D" [list [expr $x + 1] [expr $x * $x]] incr x } parray arr treat_as2D arr return } proc treat_as2D {aname} { upvar 1 $aname name foreach elem [array name name] { puts "$elem vals = 1: [lindex $name($elem) 0] 2: [lindex $name($elem) 1]" } return } And of course you can use the array indexes in very interesting ways as well. HTH Upvote 0 Downvote
Sample procedures: proc D2 {} { set x 0 array set arr {} while {$x < 200} { set arr($x,"2D" [list [expr $x + 1] [expr $x * $x]] incr x } parray arr treat_as2D arr return } proc treat_as2D {aname} { upvar 1 $aname name foreach elem [array name name] { puts "$elem vals = 1: [lindex $name($elem) 0] 2: [lindex $name($elem) 1]" } return } And of course you can use the array indexes in very interesting ways as well. HTH
Jan 3, 2003 #3 ulis Programmer Oct 12, 2001 429 FR To set myarray(0) with 1 & .023 : set myarray(0) [list 1 .203] or (if the two numbers are constant): set myarray(0) {1 .203} To get 1 from myarray(0): set value [lindex $myarray(0) 0] To get .203 from myarray(0): set value [lindex $myarray(0) 1] The value assigned to myarray(0) is a list. The command 'list' creates a list. The command 'lindex' gets the nth value inside the list. myarray is not realy an array but an associative array. If you prefer you can use a structured index: set myarray(0,0) 1 set myarray(0,1) .203 set value $myarray(0,0) (-> 1) set value $myarray(0,1) (-> .203) If you append the values in the index order, you can also use only lists (can be more efficient): set mylist {} lappend mylist [list 1 .203] set value [lindex [lindex $mylist 0] 0] (-> 1) set value [lindex [lindex $mylist 0] 1] (-> .203) For more information: http://mini.net/tcl/listhttp://mini.net/tcl/array HTH ulis Upvote 0 Downvote
To set myarray(0) with 1 & .023 : set myarray(0) [list 1 .203] or (if the two numbers are constant): set myarray(0) {1 .203} To get 1 from myarray(0): set value [lindex $myarray(0) 0] To get .203 from myarray(0): set value [lindex $myarray(0) 1] The value assigned to myarray(0) is a list. The command 'list' creates a list. The command 'lindex' gets the nth value inside the list. myarray is not realy an array but an associative array. If you prefer you can use a structured index: set myarray(0,0) 1 set myarray(0,1) .203 set value $myarray(0,0) (-> 1) set value $myarray(0,1) (-> .203) If you append the values in the index order, you can also use only lists (can be more efficient): set mylist {} lappend mylist [list 1 .203] set value [lindex [lindex $mylist 0] 0] (-> 1) set value [lindex [lindex $mylist 0] 1] (-> .203) For more information: http://mini.net/tcl/listhttp://mini.net/tcl/array HTH ulis