cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Don’t miss your chance to experience Discovery Summit Europe at our best available rate! Early bird registration through 31 Oct.
  • Image to Table JMP extension imports single or batch images as data tables. Useful to extract curves from pictures or convert image data to curve data to use in analyses like Functional Data Explorer. Download and install the extension.

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
Neo
Neo
Level VII

How to get the index of a particular value/entry in a list?

I have the following list, how do I get the index of SplPparameter?

myList = {paramter1, parameter2, parameter3, Splparameter, parameterN, parameterEnd};

Ans is 4  in the above example. 

When it's too good to be true, it's neither
1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How to get the index of a particular value/entry in a list?

Here are a couple of ways to handle your question.

Names Default To Here( 1 );
// My solution when list items are expressions
myList = {paramter1, parameter2, parameter3, Splparameter, parameterN, parameterEnd};

For( i = 1, i <= N Items( mylist ), i++,
	If( myList[i] == Parse( "Splparameter" ),
		Break()
	)
);
Show( i );

// If elements are strings the contains can be used
myList = {"paramter1", "parameter2", "parameter3", "Splparameter", "parameterN", "parameterEnd"};
i = Contains( myList, "Splparameter" );

//  Same using Loc() function
i = loc(mylist,"Splparameter" );
Jim

View solution in original post

3 REPLIES 3
txnelson
Super User

Re: How to get the index of a particular value/entry in a list?

Here are a couple of ways to handle your question.

Names Default To Here( 1 );
// My solution when list items are expressions
myList = {paramter1, parameter2, parameter3, Splparameter, parameterN, parameterEnd};

For( i = 1, i <= N Items( mylist ), i++,
	If( myList[i] == Parse( "Splparameter" ),
		Break()
	)
);
Show( i );

// If elements are strings the contains can be used
myList = {"paramter1", "parameter2", "parameter3", "Splparameter", "parameterN", "parameterEnd"};
i = Contains( myList, "Splparameter" );

//  Same using Loc() function
i = loc(mylist,"Splparameter" );
Jim
ian_jmp
Level X

Re: How to get the index of a particular value/entry in a list?

In the case of expressions, you can also use 'Loc()' (which returns a column vector):

Names Default To Here( 1 );

myList = {paramter1, parameter2, parameter3, Splparameter, parameterN, parameterEnd, Splparameter};
Print(loc(mylist, Expr(Splparameter)));

Re: How to get the index of a particular value/entry in a list?

If you know that the items are expressions, then this shorter code will work.

 

Names Default To Here( 1 );
// My solution when list items are expressions
myList = {paramter1, parameter2, parameter3, Splparameter, parameterN, parameterEnd};
Contains( myList, Expr( Splparameter ) );

 

If not, then you need a more general code like the one provided by @txnelson 

Recommended Articles