Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using arrays

0 views
Skip to first unread message

PJ Lafontaine

unread,
Jan 26, 2001, 4:25:03 PM1/26/01
to
From Cliff below .. This seems very cumbersome to set up an array. For
example I need to set up 15 different arrays and then do computations on
them. For example each array will consist of a series of 87 integers
(different values in ea array):
31,5,16,8,4,2,1,1,1,1,2,5,5,3,9,9,4,13,13,5,17,17,6,21,21,7,25,25,8,29,29,9,33,33,10,37,37,11,41,41,

12,45,45,13,49,49,14,53,53,15,57,57,16,61,61,17,1,5,18,9,13,19,17,21,20,25,29,21,33,37,22,41,45,

23,49,53,24,57,61,25,1,13,26,17,29,27,33,45,28,49,61,29,1,29,30,33,61

Now to set this where the index will run from 0-87. I can't see myself
using 87 versions of
set array(0) 31 ... 15 different times.

or do I use (note I only use () to identify the pairs..)

array set array1 (0 31) (1 5) (2 16) (3 8) (4 4) ..... (87 61)

and with the exception of the () is the above in the correct format??

Thanks for any help

Clif Flynt wrote:

> ReDiCuori wrote:
>
> > How to set one static array ? (a [10])
> > set dinamic array it's possible ?
> > set record ?
> >
>
> I'll assume you are asking about setting arrays in a
> Tcl script, not within entension C code.
>
> The trick is that Tcl has associative arrays, not chunks
> of memory used as a linear array. Array elements are
> created on demand, as you assign a value to them. Thus,
> all arrays in Tcl are dynamic.
>
> If you need to make a 10 digit array, you just need to
> assign the values to array(0), array(1), etc.
>
> for {set i 0} {$i < 0} {incr i} {set array($i) $i}
>
> Now, to get more pedantic. While you can use an
> associative array just like a C or Fortran array, the
> real power comes when you use it with string indices.
>
> For example, you can duplicate the logical data
> organization you'd use a struct for in C with associative
> arrays in Tcl:
>
> C - Structure:
> struct {
> int value;
> char description[80];
> } var;
> var.value = 1;
> strcpy(var.description, "First");
>
> Tcl Equivalent:
>
> set var(value) 1
> set var(description) "First"
>
> Finally, if you really need fixed size arrays of
> numbers for some reason, look at the vector
> data type in the BLT extension.
>
> --
> ........................... Clif Flynt ..........................
> ... Tcl/Tk for Real Programmers - Academic Press Professional ...
> .... http://www.cflynt.com ............ cl...@cflynt.com ....
> . In theory there is no difference between theory and practice .
> ........................ In practice, there is. .................

Bruce Hartweg

unread,
Jan 26, 2001, 4:38:28 PM1/26/01
to

PJ Lafontaine wrote:

> From Cliff below .. This seems very cumbersome to set up an array. For
> example I need to set up 15 different arrays and then do computations on
> them. For example each array will consist of a series of 87 integers
> (different values in ea array):
> 31,5,16,8,4,2,1,1,1,1,2,5,5,3,9,9,4,13,13,5,17,17,6,21,21,7,25,25,8,29,29,9,33,33,10,37,37,11,41,41,
>
> 12,45,45,13,49,49,14,53,53,15,57,57,16,61,61,17,1,5,18,9,13,19,17,21,20,25,29,21,33,37,22,41,45,
>
> 23,49,53,24,57,61,25,1,13,26,17,29,27,33,45,28,49,61,29,1,29,30,33,61
>
> Now to set this where the index will run from 0-87. I can't see myself
> using 87 versions of
> set array(0) 31 ... 15 different times.
>
> or do I use (note I only use () to identify the pairs..)
>
> array set array1 (0 31) (1 5) (2 16) (3 8) (4 4) ..... (87 61)
>
> and with the exception of the () is the above in the correct format??

close, the array set command expects the name value pairs a a list.

array set array1 {0 31 1 5 2 16 3 8 4 4 ..... 87 61}

although, it might be simpler to just use lists instead of an array

set data1 {31 5 16 8 4 2 1 1 1 ....... 30 33 61}

and the use [lindex $data1 $i] to get a specific value (where i is 0 - 86)

and if you are doing something that needs to be done on all elements you can use foreach
to iterate through them

e.g.

set sum {}
foreach a $data1 b $data2 c $data3 {
lappend sum [expr $a + $b + $c]
}

hope it helps.

Bruce


PJ Lafontaine

unread,
Jan 28, 2001, 4:56:13 PM1/28/01
to
Thanks Bruce. Once I got around the differences in llength, lreplace and lappend using the list was the
easiest way. Thanks for the help..

pat

0 new messages