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 |
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 |
In reply to this post by L'assegnista
> On Jan 9, 2021, at 7:04 PM, Federico Luzzati <[hidden email]> 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. Arrays can be filled in functions, as in this example: a1 = newArray(5); a2 = newArray(5); fillArray(a1, 3); Array.print(a1); fillArray(a2, 7); Array.print(a2); function fillArray(a,v) { Array.fill(a,v); } The output is: 3, 3, 3, 3, 3 7, 7, 7, 7, 7 -wayne > 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 -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Dear all,
thank you very much for your quick and informative answers. Indeed, all the suggestions solved my problem. As I have many arrays it is less handy to add them in the function arguments ( that is already crowded!) or as a return. For this I will go with the Michael suggestion of defining the arrays as global arrays, outside the function. Thank you very much indeed to you all!! Greetings Federico Il giorno dom 10 gen 2021 alle ore 07:52 Wayne Rasband <[hidden email]> ha scritto: > > On Jan 9, 2021, at 7:04 PM, Federico Luzzati <[hidden email]> > 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. > > Arrays can be filled in functions, as in this example: > > a1 = newArray(5); > a2 = newArray(5); > fillArray(a1, 3); > Array.print(a1); > fillArray(a2, 7); > Array.print(a2); > > function fillArray(a,v) { > Array.fill(a,v); > } > > The output is: > > 3, 3, 3, 3, 3 > 7, 7, 7, 7, 7 > > -wayne > > > 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 > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- 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 |
Free forum by Nabble | Edit this page |