Login  Register

Re: In a macro, how can I index upon an array whose name and length are passed to a function?

Posted by Bill Christens-Barry on Jan 29, 2009; 8:47pm
URL: http://imagej.273.s1.nabble.com/In-a-macro-how-can-I-index-upon-an-array-whose-name-and-length-are-passed-to-a-function-tp3693895p3693896.html

Thanks, Gabriel and Curtis, your suggestions worked perfectly. I should have realized passing by
reference is the way.

Bill Christens-Barry


On Thu, 29 Jan 2009 14:32:25 -0600, Curtis Rueden <[hidden email]> wrote:

>Hi Bill,
>
>Try this:
>
>-----
>a = newArray(1, 2, 3);
>b = newArray(4, 5, 6, 7);
>
>sumTheArray(a);
>sumTheArray(b);
>
>function sumTheArray(arrayName) {
>   theArraySum = 0;
>   for (i = 0; i < lengthOf(arrayName); i++) {
>       theArraySum = theArraySum + arrayName[i];
>   }
>   print(theArraySum);
>}
>-----
>
>-Curtis
>
>On Thu, Jan 29, 2009 at 2:07 PM, Bill Christens-Barry <
>[hidden email]> wrote:
>
>> In a macro, how can I index upon an array whose name and length are passed
>> to a function? This
>> nonworking example shows what I'd like to do:
>>
>> a = newArray(1, 2, 3);
>> b = newArray(4, 5, 6, 7);
>>
>> sumTheArray("a", 3);
>> sumTheArray("b", 4);
>>
>> function sumTheArray(arrayName, n) {
>>    theArraySum = 0;
>>    for (i = 0, i < n; i++) {
>>        theArraySum += arrayName[n];
>>        print(theArraySum);
>>    }
>> }
>>
>> Bill Christens-Barry
>>