array's element index

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

array's element index

Soriano.Joaquim
Hi everyone!

I got stuck programming a macro.

Does anybody know whether there is a straightforward way to know the position that a certain element of an array is occupying in such array?

Take the array bellow as an example:

MyArray (1,5,3,12)

I just want to know that the element "5" of MyArray is occupying the second position in MyArray.

Thank you very muchin advance.

 

Joaquim Soriano Felipe.

Confocal Microscopy Unit

Spanish National Cancer Research Centre (CNIO)

Melchor Fernández Almagro,3, E-28029 Madrid, Spain

Phone  + 34 917 328 000, exts.: 3161, 3162. [hidden email] <mailto:[hidden email]>

 



**NOTA DE CONFIDENCIALIDAD** Este correo electrÿnico, y en su caso los ficheros adjuntos, pueden contener informaciÿn protegida para el uso exclusivo de su destinatario. Se prohÿbe la distribuciÿn, reproducciÿn o cualquier otro tipo de transmisiÿn por parte de otra persona que no sea el destinatario. Si usted recibe por error este correo, se ruega comunicarlo al remitente y borrar el mensaje recibido.
**CONFIDENTIALITY NOTICE** This email communication and any attachments may contain confidential and privileged information for the sole use of the designated recipient named above. Distribution, reproduction or any other use of this transmission by any party other than the intended recipient is prohibited. If you are not the intended recipient please contact the sender and delete all copies.



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

Re: array's element index

Michael Schmid
Hi Joaquim,

here is a macro function for this:

arr = newArray(1,5,3,12);
i = index(arr, 5);
print("index = "+i);

  function index(a, value) {
      for (i=0; i<a.length; i++)
          if (a[i]==value) return i;
      return -1;
  }

Note that the index starts with 0 for the first position.

Michael
________________________________________________________________
On Aug 16, 2012, at 11:25, Soriano.Joaquim wrote:

> Hi everyone!
>
> I got stuck programming a macro.
>
> Does anybody know whether there is a straightforward way to know the position that a certain element of an array is occupying in such array?
>
> Take the array bellow as an example:
>
> MyArray (1,5,3,12)
>
> I just want to know that the element "5" of MyArray is occupying the second position in MyArray.
>
> Thank you very muchin advance.
>
>
>
> Joaquim Soriano Felipe.

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