cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Learn how to build custom Python data connectors and further customize JMP’s Data Connector Framework with the Python Data Connector Demo, available now in the JMP Marketplace!
  • See how to create experiments to support product design and ID useful product features. Register for June 12 webinar, 2pm US Eastern Time.

Discussions

Solve problems, and share tips and tricks with other JMP users.
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 

Recommended Articles