MACRO Language | working with Array

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

MACRO Language | working with Array

Rainer M. Engel
Hi there.

This might be rather simple. I want to work with arrays and
modify them. Most important add and remove elements.
How does it work?

Maybe I'm looking for something like this:
Array.concat(array) - concatenate/combine two arrays
Array.pop(array) - remove last element of array (+)
Array.push(array) - add new elements to array at end
Array.shift(array) - remove first element of array (+)
Array.slice(array) - extract a range of elements (+)
...

I added (+) after the mimics that I could achieve.

For example the Shift mimic does work in a transparent way but
I still want to know how to add an element.

Any help appreciated..

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*This makro creates an Array and independently from the number
  * of its elements trims the first element. ~ shift
  */

requires("1.46");//at least created by this version
print("\\Clear");
a1 = newArray(0,1,2,3);
list("Listing Array-Elements:", a1);

ALength = a1.length;
print("---------------------");
print("Array-Length: "+ALength);

// Delete first element of Array
trimBy = ALength-1;
a2 = Array.copy(a1);
a2 = Array.invert(a2);
a2 = Array.trim(a2, trimBy);
a2 = Array.invert(a2);
list("\nListing Array-Elements (first deleted):", a2);


function list(name, a) {
     print(name);
     for (i=0; i<a.length; i++)
     print(a[i]);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Regards,
Rainer

--
Rainer M. Engel
www.scientific-media.de
Reply | Threaded
Open this post in threaded view
|

Re: MACRO Language | working with Array

Rasband, Wayne (NIH/NIMH) [E]
On Dec 6, 2011, at 11:09 AM, Rainer M. Engel wrote:

> Hi there.
>
> This might be rather simple. I want to work with arrays and
> modify them. Most important add and remove elements.
> How does it work?
>
> Maybe I'm looking for something like this:
> Array.concat(array) - concatenate/combine two arrays
> Array.pop(array) - remove last element of array (+)
> Array.push(array) - add new elements to array at end
> Array.shift(array) - remove first element of array (+)
> Array.slice(array) - extract a range of elements (+)
> ...
>
> I added (+) after the mimics that I could achieve.
>
> For example the Shift mimic does work in a transparent way but
> I still want to know how to add an element.
>
> Any help appreciated.

The ImageJ 1.46c daily build adds three new macro functions:

   Array.concat(a1,a2) - concatenates two or more arrays
   Array.slice(a1,start,end) - extracts a range of elements
   Array.print(a) - prints an array

Use Array.concat() to add (push) an element to the end of an array:

   a = Array.concat(a,newElement)

And use Array.slice() to remove the first element of an array:

     a = Array.slice(a,1);

There are more examples on the ImageJ website at:

   http://imagej.nih.gov/ij/macros/examples/ArrayConcatExamples.txt
   http://imagej.nih.gov/ij/macros/examples/ArraySliceExamples.txt

-wayne


> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> /*This makro creates an Array and independently from the number
>  * of its elements trims the first element. ~ shift
>  */
>
> requires("1.46");//at least created by this version
> print("\\Clear");
> a1 = newArray(0,1,2,3);
> list("Listing Array-Elements:", a1);
>
> ALength = a1.length;
> print("---------------------");
> print("Array-Length: "+ALength);
>
> // Delete first element of Array
> trimBy = ALength-1;
> a2 = Array.copy(a1);
> a2 = Array.invert(a2);
> a2 = Array.trim(a2, trimBy);
> a2 = Array.invert(a2);
> list("\nListing Array-Elements (first deleted):", a2);
>
>
> function list(name, a) {
>     print(name);
>     for (i=0; i<a.length; i++)
>     print(a[i]);
> }
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
>
> Regards,
> Rainer
>
> --
> Rainer M. Engel
> www.scientific-media.de
Reply | Threaded
Open this post in threaded view
|

Re: MACRO Language | working with Array SOLVED

Rainer M. Engel
Tremendous.. That is great news. Thank you Wayne.

Besides I found some Macros with built-in functions on this page:
http://www.richardwheeler.net/contentpages/textgallery.php?gallery=ImageJ_Macros

But hey, you were so fast I even couldn't give it a try yesterday ;-)

Best Regards,
Rainer


--
Rainer M. Engel
www.scientific-media.de