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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Bill Christens-Barry
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
Reply | Threaded
Open this post in threaded view
|

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

ctrueden
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
>
Reply | Threaded
Open this post in threaded view
|

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

Gabriel Landini
In reply to this post by Bill Christens-Barry
On Thursday 29 January 2009 20:07:36 Bill Christens-Barry 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:

Maybe this is what you want:

a = newArray(1, 2, 3);
b = newArray(4, 5, 6, 7);

sumTheArray(a);
sumTheArray(b);

function sumTheArray(aname) {
    theArraySum = 0;
    for (i = 0; i < aname.length; i++) {
        theArraySum += aname[i];
    }
     print(theArraySum);
}


Cheers

G.
Reply | Threaded
Open this post in threaded view
|

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

Bill Christens-Barry
In reply to this post by Bill Christens-Barry
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
>>