macro creating n Arrays within a "for"-loop

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

macro creating n Arrays within a "for"-loop

Johannes-P. Koch
Dear Listers,

I am trying to create a number of arrays from within a "for"-loop. The
problem is, that the actual number of arrays depends on something else,
which means it is not known a priori. And the arrays should be called
after the loop is finished. So, just to create an array and use it within
the loop, does not work for me. The best would be to name it, let's say as
"test" and then add a number for each new array created, test 1, test 2,
...
However, the following did not seem to work....as it seems to redefine the
string "name"....

for (i=0; i<n; i++) {

     name = "test " +i;
     name = newArray(k);

}

and this is not accepted anyway...



for (i=0; i<n; i++) {

     "test " +i = newArray(k);

}


Any ideas or workarounds?

Thanks a lot, every help is very much appreciated.

Johannes
Reply | Threaded
Open this post in threaded view
|

Re: macro creating n Arrays within a "for"-loop

Gabriel Landini
>      name = "test " +i;
>      name = newArray(k);

Maybe because you are trying to make a variable name that contains a space.
Try removing it.

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

Re: macro creating n Arrays within a "for"-loop

Johannes-P. Koch
Unfortunately not; I suppose, the second line defines name as an array,
thus ignoring the first line...meaning that I only get ONE array after the
loop...anyhow, I managed to implement everything in the loop; it is now a
little bit weired, but it works fine for me...

Johannes

On So, 2.05.2010, 16:14, Gabriel Landini wrote:
>>      name = "test " +i;
>>      name = newArray(k);
>
> Maybe because you are trying to make a variable name that contains a
> space.
> Try removing it.
>
> G.
>
Reply | Threaded
Open this post in threaded view
|

Antwort: Re: macro creating n Arrays within a "for"-loop

Joachim Wesner
In reply to this post by Gabriel Landini
>>      name = "test " +i;
>>      name = newArray(k);

>Maybe because you are trying to make a variable name that contains a
space.
>Try removing it.

No, this will only create a new Variable called "name" of type Array, the
old string variable "name" will be discarded!

As long as there is no "execute" statement in the ImageJ Macro language
(Hint! Hint!), that really RE-interprets a string as a new command
(however, if I´m not mistaken here, you would need thsi also in all places,
where you need to access one the the multiple arrays,
not only when creating those) you are somewhere out of luck.

Johannes,

What I would do in such cases, if you already know the number of "arrays" n
outside the loop (can still be a dynamic value that is read from
somewhere on runtime, you could create an array of n-times the single size
and use a variable offset in that array.

(Maybe this is basically what you did?)


Cheers

Joachim Wesner



______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________
Reply | Threaded
Open this post in threaded view
|

Re: macro creating n Arrays within a "for"-loop

lechristophe
I think there is an "execute" command in the macro language that reads
a string as a command. It is the "eval" function :

eval(macro)
Evaluates (runs) one or more lines of macro code. An optional second
argument can be used to pass a string to the macro being evaluated.
See also: EvalDemo macro and runMacro function.

http://rsbweb.nih.gov/ij/developer/macro/functions.html#eval

Or maybe I make a confusion ? I used it recently to create a segmented
line from an array of coordinates, by concatenating a MakeLine command
with all coordinates as a string, and using "eval" to execute it.

Christophe

On Fri, May 7, 2010 at 13:20, Joachim Wesner
<[hidden email]> wrote:

>>>      name = "test " +i;
>>>      name = newArray(k);
>
>>Maybe because you are trying to make a variable name that contains a
> space.
>>Try removing it.
>
> No, this will only create a new Variable called "name" of type Array, the
> old string variable "name" will be discarded!
>
> As long as there is no "execute" statement in the ImageJ Macro language
> (Hint! Hint!), that really RE-interprets a string as a new command
> (however, if I´m not mistaken here, you would need thsi also in all places,
> where you need to access one the the multiple arrays,
> not only when creating those) you are somewhere out of luck.
>
> Johannes,
>
> What I would do in such cases, if you already know the number of "arrays" n
> outside the loop (can still be a dynamic value that is read from
> somewhere on runtime, you could create an array of n-times the single size
> and use a variable offset in that array.
>
> (Maybe this is basically what you did?)
>
>
> Cheers
>
> Joachim Wesner
>
>
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
Reply | Threaded
Open this post in threaded view
|

Antwort: Re: macro creating n Arrays within a "for"-loop

Joachim Wesner
Hi Cristophe,

yep, you are right, but I fear, this feature alone would still make any
code very clumsy,
what one would really need is some kind "indirection" that really takes a
string
variable and interprets it´s contents in the same places (within an
expression
and also to the left of the assignment) and the same way as the variable
that´s
"in" the string (including indexing... etc.)

Joachim

ImageJ Interest Group <[hidden email]> schrieb am 07.05.2010 13:57:43:

> I think there is an "execute" command in the macro language that reads
> a string as a command. It is the "eval" function :
>
> eval(macro)
> Evaluates (runs) one or more lines of macro code. An optional second
> argument can be used to pass a string to the macro being evaluated.
> See also: EvalDemo macro and runMacro function.
>
> http://rsbweb.nih.gov/ij/developer/macro/functions.html#eval
>
> Or maybe I make a confusion ? I used it recently to create a segmented
> line from an array of coordinates, by concatenating a MakeLine command
> with all coordinates as a string, and using "eval" to execute it.
>
> Christophe
>
> On Fri, May 7, 2010 at 13:20, Joachim Wesner
> <[hidden email]> wrote:
> >>>      name = "test " +i;
> >>>      name = newArray(k);
> >
> >>Maybe because you are trying to make a variable name that contains a
> > space.
> >>Try removing it.
> >
> > No, this will only create a new Variable called "name" of type Array,
the
> > old string variable "name" will be discarded!
> >
> > As long as there is no "execute" statement in the ImageJ Macro language
> > (Hint! Hint!), that really RE-interprets a string as a new command
> > (however, if I´m not mistaken here, you would need thsi also in all
places,
> > where you need to access one the the multiple arrays,
> > not only when creating those) you are somewhere out of luck.
> >
> > Johannes,
> >
> > What I would do in such cases, if you already know the number of
"arrays" n
> > outside the loop (can still be a dynamic value that is read from
> > somewhere on runtime, you could create an array of n-times the single
size

> > and use a variable offset in that array.
> >
> > (Maybe this is basically what you did?)
> >
> >
> > Cheers
> >
> > Joachim Wesner
> >
> >
> >
> > ______________________________________________________________________
> > This email has been scanned by the MessageLabs Email Security System.
> > For more information please visit http://www.messagelabs.com/email
> > ______________________________________________________________________
> >


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________
Reply | Threaded
Open this post in threaded view
|

Re: Antwort: Re: macro creating n Arrays within a "for"-loop

Johannes-P. Koch
Joachim,

thanks for your reply! Yes, indeed, that is exactly the solution I came up
with. However, the code is a now a little bit clumsy, especially since I
am not really into programming. Still, it works very fine for my
application!

Johannes

On Fr, 7.05.2010, 14:18, Joachim Wesner wrote:

> Hi Cristophe,
>
> yep, you are right, but I fear, this feature alone would still make any
> code very clumsy,
> what one would really need is some kind "indirection" that really takes a
> string
> variable and interprets it´s contents in the same places (within an
> expression
> and also to the left of the assignment) and the same way as the variable
> that´s
> "in" the string (including indexing... etc.)
>
> Joachim
>
> ImageJ Interest Group <[hidden email]> schrieb am 07.05.2010
> 13:57:43:
>
>> I think there is an "execute" command in the macro language that reads
>> a string as a command. It is the "eval" function :
>>
>> eval(macro)
>> Evaluates (runs) one or more lines of macro code. An optional second
>> argument can be used to pass a string to the macro being evaluated.
>> See also: EvalDemo macro and runMacro function.
>>
>> http://rsbweb.nih.gov/ij/developer/macro/functions.html#eval
>>
>> Or maybe I make a confusion ? I used it recently to create a segmented
>> line from an array of coordinates, by concatenating a MakeLine command
>> with all coordinates as a string, and using "eval" to execute it.
>>
>> Christophe
>>
>> On Fri, May 7, 2010 at 13:20, Joachim Wesner
>> <[hidden email]> wrote:
>> >>>      name = "test " +i;
>> >>>      name = newArray(k);
>> >
>> >>Maybe because you are trying to make a variable name that contains a
>> > space.
>> >>Try removing it.
>> >
>> > No, this will only create a new Variable called "name" of type Array,
> the
>> > old string variable "name" will be discarded!
>> >
>> > As long as there is no "execute" statement in the ImageJ Macro
>> language
>> > (Hint! Hint!), that really RE-interprets a string as a new command
>> > (however, if I´m not mistaken here, you would need thsi also in all
> places,
>> > where you need to access one the the multiple arrays,
>> > not only when creating those) you are somewhere out of luck.
>> >
>> > Johannes,
>> >
>> > What I would do in such cases, if you already know the number of
> "arrays" n
>> > outside the loop (can still be a dynamic value that is read from
>> > somewhere on runtime, you could create an array of n-times the single
> size
>> > and use a variable offset in that array.
>> >
>> > (Maybe this is basically what you did?)
>> >
>> >
>> > Cheers
>> >
>> > Joachim Wesner
>> >
>> >
>> >
>> > ______________________________________________________________________
>> > This email has been scanned by the MessageLabs Email Security System.
>> > For more information please visit http://www.messagelabs.com/email
>> > ______________________________________________________________________
>> >
>
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
>
Reply | Threaded
Open this post in threaded view
|

Re: macro creating n Arrays within a "for"-loop

Chris Mawata
In reply to this post by Johannes-P. Koch
You need an ArrayList<int[ ]>. If the number of arrays is not known
until you are inside the loop
then use a while loop and get out when the number of arrays is the
appropriate number.

Example code ; CreateArrays.java


import java.util.Random;
import java.util.ArrayList;

public class CreateArrays{

     public static void main(String... args){
         Random random = new Random();

         //This will come from some calculation or other
         int numberOfArrays = random.nextInt(10)+1;


         ArrayList<int[]> allArrays = new ArrayList<int[]>();

         for(int i=0; i<numberOfArrays; i++){
             //The size of the ithe array will come from some calculation
             int sizeOfIthArray = random.nextInt(5)+1;
             int[] ithArray=new int[sizeOfIthArray];
             //populate the ith array
             for(int j=0; j< sizeOfIthArray; j++){
                 ithArray[j]=random.nextInt(100);
             }
             //put it int the ArrayList
             allArrays.add(ithArray);
         }

         //Sanity check. If you just need array number i use the syntax
allArrays.get(i).
         System.out.println("We have "+ allArrays.size()+" arrays.\n");
         for(int[] ithArray : allArrays){
             System.out.print("[\t");
             for(int entry : ithArray){
                 System.out.print(entry+"\t");
             }
             System.out.println("]");
         }

     }

}

//end example code

Chris Mawata

On 5/2/2010 10:03 AM, Johannes-Paul M. Koch wrote:

> Dear Listers,
>
> I am trying to create a number of arrays from within a "for"-loop. The
> problem is, that the actual number of arrays depends on something else,
> which means it is not known a priori. And the arrays should be called
> after the loop is finished. So, just to create an array and use it within
> the loop, does not work for me. The best would be to name it, let's say as
> "test" and then add a number for each new array created, test 1, test 2,
> ...
> However, the following did not seem to work....as it seems to redefine the
> string "name"....
>
> for (i=0; i<n; i++) {
>
>       name = "test " +i;
>       name = newArray(k);
>
> }
>
> and this is not accepted anyway...
>
>
>
> for (i=0; i<n; i++) {
>
>       "test " +i = newArray(k);
>
> }
>
>
> Any ideas or workarounds?
>
> Thanks a lot, every help is very much appreciated.
>
> Johannes
>
>    
Reply | Threaded
Open this post in threaded view
|

Re: Antwort: Re: macro creating n Arrays within a "for"-loop

Chris Mawata
In reply to this post by Joachim Wesner
How about a HashMap<String, Integer> to maintain a mapping of the i in
th array to the
String actual name. I am thinking of creating a method like
getAraay(String name) which
then gives you     allArrays.get(myHashmap.get(name)).


On 5/7/2010 8:18 AM, Joachim Wesner wrote:

> Hi Cristophe,
>
> yep, you are right, but I fear, this feature alone would still make any
> code very clumsy,
> what one would really need is some kind "indirection" that really takes a
> string
> variable and interprets it´s contents in the same places (within an
> expression
> and also to the left of the assignment) and the same way as the variable
> that´s
> "in" the string (including indexing... etc.)
>
> Joachim
>
> ImageJ Interest Group<[hidden email]>  schrieb am 07.05.2010 13:57:43:
>
>    
>> I think there is an "execute" command in the macro language that reads
>> a string as a command. It is the "eval" function :
>>
>> eval(macro)
>> Evaluates (runs) one or more lines of macro code. An optional second
>> argument can be used to pass a string to the macro being evaluated.
>> See also: EvalDemo macro and runMacro function.
>>
>> http://rsbweb.nih.gov/ij/developer/macro/functions.html#eval
>>
>> Or maybe I make a confusion ? I used it recently to create a segmented
>> line from an array of coordinates, by concatenating a MakeLine command
>> with all coordinates as a string, and using "eval" to execute it.
>>
>> Christophe
>>
>> On Fri, May 7, 2010 at 13:20, Joachim Wesner
>> <[hidden email]>  wrote:
>>      
>>>>>       name = "test " +i;
>>>>>       name = newArray(k);
>>>>>            
>>>        
>>>> Maybe because you are trying to make a variable name that contains a
>>>>          
>>> space.
>>>        
>>>> Try removing it.
>>>>          
>>> No, this will only create a new Variable called "name" of type Array,
>>>        
> the
>    
>>> old string variable "name" will be discarded!
>>>
>>> As long as there is no "execute" statement in the ImageJ Macro language
>>> (Hint! Hint!), that really RE-interprets a string as a new command
>>> (however, if I´m not mistaken here, you would need thsi also in all
>>>        
> places,
>    
>>> where you need to access one the the multiple arrays,
>>> not only when creating those) you are somewhere out of luck.
>>>
>>> Johannes,
>>>
>>> What I would do in such cases, if you already know the number of
>>>        
> "arrays" n
>    
>>> outside the loop (can still be a dynamic value that is read from
>>> somewhere on runtime, you could create an array of n-times the single
>>>        
> size
>    
>>> and use a variable offset in that array.
>>>
>>> (Maybe this is basically what you did?)
>>>
>>>
>>> Cheers
>>>
>>> Joachim Wesner
>>>
>>>
>>>
>>> ______________________________________________________________________
>>> This email has been scanned by the MessageLabs Email Security System.
>>> For more information please visit http://www.messagelabs.com/email
>>> ______________________________________________________________________
>>>
>>>        
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
>    
Reply | Threaded
Open this post in threaded view
|

Re: Antwort: Re: macro creating n Arrays within a "for"-loop

Sylvia Neumann
In reply to this post by Joachim Wesner
Hi Joachim,

I am trying to do something similar and don't really understand what a variable offset is. Could you write an example of that solution? That would be a great help! Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: macro creating n Arrays within a "for"-loop

Michael Schmid
Hi Sylvia,

as I understand it, the idea for packing several arrays of different
length into one big array is something like the following:


// This macro creates a big array containing several smaller ones
macro 'SmallArrays in Big Array' {
   //Create the arrays and fill them
   nArray = 7;  //we have 7 small arrays
   bigArray = newArray(1000);     //ImageJ extends it if required
   offsets = newArray(nArray+1);  //small array beginnings in bigArray
   for (iArray=0; iArray<nArray; iArray++) {
     smallArraysize = 2+round(2*random); //whatever size we want
     for (i=0; i<smallArraysize; i++) {
       value = i*i; //whatever value we want to save
       putArrayElement(bigArray, offsets, iArray, i, value);
     }
   }

   //Read the arrays
   for (iArray=0; iArray<nArray; iArray++) {
     print("==== Array number ", iArray, "====");
     smallArraysize = getArrayLength(iArray);
     for (i=0; i<smallArraysize; i++) {
       value = getArrayElement(bigArray, offsets, iArray, i);
       print ("element ", i, ": ", value);
     }
   }
}

   // Writes the i-th value into array number 'iArray'
   // Works correctly only if the small arrays are filled
   // in increasing sequence (starting with all elements of
   // iArray = 0, then all elements of iArray = 1, etc.
   // Later modification of array elements are allowed
   // as long as the array sizes are not extended.
   function putArrayElement(bigArray, offsets, iArray, i, value) {
     if (iArray >= lengthOf(offsets)-1)
       exit("No small Array #"+iArray);
     index = offsets[iArray] + i;
     bigArray[index] = value;
     if (offsets[iArray+1] < index+1)
       offsets[iArray+1] = index+1;
   }

   // Reads the i-th value into array number 'iArray'
   function getArrayElement(bigArray, offsets, iArray, i) {
     if (iArray >= lengthOf(offsets)-1)
       exit("No small Array #"+iArray);
     if (i >= offsets[iArray+1] - offsets[iArray])
       exit("Small Array "+iArray+" has no #"+i);
     index = offsets[iArray] + i;
     return bigArray[index];
   }

   // Gives the length of the given array
   function getArrayLength(iArray) {
     return offsets[iArray+1] - offsets[iArray];
   }


Michael
________________________________________________________________
On 2016-07-08 23:45, Sylvia Neumann wrote:
> Hi Joachim,
>
> I am trying to do something similar and don't really understand what a
> variable offset is. Could you write an example of that solution? That would
> be a great help! Thank you.
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/macro-creating-n-Arrays-within-a-for-loop-tp3688201p5016835.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html