Posted by
Fred Damen on
Jan 10, 2021; 2:31am
URL: http://imagej.273.s1.nabble.com/Filling-an-Array-from-within-a-user-defined-function-in-Macro-Language-tp5024345p5024346.html
Greetings Federico,
The arrays are actually filled in, although they are not the arrays you
think they are, see my response about why the counter counts of a couple
of days ago.
N.B.: passing in the array as a parameter does not help, as the macro
language seems to pass arrays by value and not by reference.
To get what you want you need to pass the array back to the caller:
function func() {
ga = newArray();
for(i=0; i<5; i++)
ga=Array.concat(ga,i);
return ga;
}
ga=func();
print("ga.length = "+ga.length);
output:
ga.length = 5
If your array is really long, you may want to prealloc/overalloc the array
and keep count of the actual count.
Enjoy,
Fred
On Sat, January 9, 2021 6:04 pm, Federico Luzzati wrote:
> Dear List,
> I have a long macro that I tried to simplify using a function.
> The main purpose of this function was to fill a series of arrays that I'd
> like to save at the end of the macro.
> However I saw that arrays are not filled within functions and I was
> wondering if this could be possible. I have very little knowledge of
> programming in general ( as you could have already understood!)
>
> Here a quick example that demonstrate what I would like to do
>
> Array1=newArray();
> Array2=newArray();
>
> for (i=0;i<5;i++){
> Array1=Array.concat(Array1,i);
> }
>
> function Array_fill(n){
> for(i=0;i<n;i++){
> Array2=Array.concat(Array2,i);
> }
> }
> Array_fill(5);
> print("Array1 length: "+Array1.length);
> print("Array2 length: "+Array2.length);
>
> Thank you very much
>
> Federico
>
> --
> Federico Luzzati, PhD
> Assistant Professor
> University of Turin,
> Dept. LIfe Sciences and Systems Biology (DBIOS)
> Via Accademia Albertina, 13 - 10123 Torino - IT
>
> Researcher at
> Neuroscience Institute Cavalieri Ottolenghi (NICO)
> Regione Gonzole, 10 - 10043 Orbassano (To) - IT
>
> tel. +39-0116704683/ -6631
>
http://www.nico.ottolenghi.unito.it/index.php/it/adult-neurogenesis>
> Sostieni con il tuo 5xmille il NICO e la ricerca sulle malattie
> neurodegenerative
> Firma nel riquadro "Finanziamento della ricerca scientifica e
> dell'Università "
> CF 97564560015
>
> --
> ImageJ mailing list:
http://imagej.nih.gov/ij/list.html>
--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html