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
JMP2021
Level III

How to extract the position of a char in a string?

I have the following column, which contains string of char, such as dot or char (no number and special char).

I want to generate a column that list out the position of the char which is not a dot. See an example below.

Is there any formula or script I can use in JMP??

 

·cgaagt··a···········           2,3,4,5,6,7,10
··t·a·············c··              3,5,19
·····················               

2 REPLIES 2
jthi
Super User

Re: How to extract the position of a char in a string?

I think you will have to script it. 

  1. Convert the string to a list with Words(str, "")
  2. Replace all values you aren't interested in with empty string (missing value) by using Substitute
  3. Use Loc NonMissing to find indices of non missing values

 

Names Default To Here(1);

a = "·cgaagt··a···········";
b = "··t·a·············c··";
c = "·····················";
list_of_chars = Words(a, "");
Substitute Into(list_of_chars, "·", "");
Show(AsList(Loc Nonmissing(list_of_chars)));

a = "··t·a·············c··";
list_of_chars = Words(a, "");
Substitute Into(list_of_chars, "·", "");
Show(AsList(Loc Nonmissing(list_of_chars)));

a = "·····················";
list_of_chars = Words(a, "");
Substitute Into(list_of_chars, "·", "");
Show(AsList(Loc Nonmissing(list_of_chars)));

//As List(Loc Nonmissing(list_of_chars)) = {2, 3, 4, 5, 6, 7, 10};
//As List(Loc Nonmissing(list_of_chars)) = {3, 5, 19};
//As List(Loc Nonmissing(list_of_chars)) = {};
-Jarmo
txnelson
Super User

Re: How to extract the position of a char in a string?

Expanding on @jthi great solution, I believe the best way to handle your issue is to create a new column to place the results in.  Also, you indicated that not just a "." might be used as a special character.  So the JSL below is a formula that can handle multiple special characters, and also returns the results in a new column.  Just create a character column and cut and past the formula into it.

txnelson_0-1641971637056.png

list_of_chars = Words( :strings, "" );
Substitute Into( list_of_chars, ".", "", "+", "", "/", "" );
charList = As List( Loc Nonmissing( list_of_chars ) );
Substr( Char( charList ), 2, Length( Char( charList ) ) - 2 );

 

 

Jim