cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
Neo
Neo
Level VI

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