Re: continue equivalent on macro language (for loop control)

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

Re: continue equivalent on macro language (for loop control)

Rasband, Wayne (NIH/NIMH) [E]
On Mar 15, 2011, at 6:13 PM, Carnë Draug wrote:

> Hi everyone,
>
> is there any equivalent to 'continue' in a for loop in the macro
> language? I'm inside a foor loop and want to skip to the next value in
> the loop if a certain condition is not meet. Here's a bit of code
> example
>
> // get list of files and options
>
> for (i = 0; i < lengthOf(file_list); i++) {
>  // check if file exists before opening. Skip if it's no longer there
>  if(File.exists(file_list[i]) {
>    file.open(file_list[i]);
>  } else {
>    print ("File disappeared. Skipping");
>    continue;
>  }
>  // Do very smart image analysis here
> }
>
> I tried continue and it doesn't work. "next" also doesn't. Any help
> would be appreciated.

ImageJ 1.48d adds support for 'continue' and 'break' statements to the macro language.

-wayne

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: continue equivalent on macro language (for loop control)

lechristophe
Hi Carnë and Wayne,

Just a side note for Carnë and other macro-writing listers:  it's possible
to use functions for this, using a conditional return when the condition
isn't met. Functions usually make your code much easier to manipulate.

so that would be (it's just quick and dirty pseudocode, there must be some
syntax errors in it):

macro "my_macro" {
 for (i = 0; i < lengthOf(file_list); i++) {
  treatFile(file_list[i]);
 }
}


function treatFile(path){
 // check if file exists before opening. Skip if it's no longer there
 if(File.exists(path) {
  file.open(path);
  }
 else {
  print ("File disappeared. Skipping");
  return;
 }
  // Do very smart image analysis here
}


Cheers,

Christophe


On Sun, Sep 29, 2013 at 6:34 AM, Rasband, Wayne (NIH/NIMH) [E] <
[hidden email]> wrote:

> On Mar 15, 2011, at 6:13 PM, Carnë Draug wrote:
>
> > Hi everyone,
> >
> > is there any equivalent to 'continue' in a for loop in the macro
> > language? I'm inside a foor loop and want to skip to the next value in
> > the loop if a certain condition is not meet. Here's a bit of code
> > example
> >
> > // get list of files and options
> >
> > for (i = 0; i < lengthOf(file_list); i++) {
> >  // check if file exists before opening. Skip if it's no longer there
> >  if(File.exists(file_list[i]) {
> >    file.open(file_list[i]);
> >  } else {
> >    print ("File disappeared. Skipping");
> >    continue;
> >  }
> >  // Do very smart image analysis here
> > }
> >
> > I tried continue and it doesn't work. "next" also doesn't. Any help
> > would be appreciated.
>
> ImageJ 1.48d adds support for 'continue' and 'break' statements to the
> macro language.
>
> -wayne
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: continue equivalent on macro language (for loop control)

Rainer M. Engel
>> ImageJ 1.48d adds support for 'continue' and 'break' statements to the
>> macro language.
>>
>> -wayne
>>

Nice coincidence, I was in the mood of asking for that too. Thank you
Carnë and Wayne.


// loop functions (examples)
//+++++++++++++++++++++++++++++++++++++++++++++++++
a = newArray(0,0,0,1,0);
c = 0;
for (i=0; i<a.length; i++) {
        //print(a[i]);
        if (a[i] != 0) {
                //continue; //continues to next element of loop
                break; //breaks the loop and continues macro
        }
        print("element is: 0");
        c++;
}
print("end of macro reached, "+c+" elements printed");
//+++++++++++++++++++++++++++++++++++++++++++++++++

Like Christophe wrote, there are other ways to skip elements inside a
loop but the new functions offer far better ways. Very nice..

Regards,
Rainer


--
Rainer M. Engel, Dipl. Digital Artist
scientific|Media GbR
Pichelsdorfer Str. 143
13595 Berlin

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