Share your ideas for the JMP Scripting Unsession at Discovery Summit by September 17th. We hope to see you there!
Choose Language Hide Translation Bar
Highlighted
nikles
Level IV

Make "Words" function respect consecutive limiters?

Hi.  Suppose I have a string "cat,dog,,,,mouse,".  Is there a simple way to break this string into a list of substrings in such a way that the result is {"cat", "dog", "", "", "", "mouse", ""}?  I've been using the Words() function, but it seems to treat consecutive delimiters in series as just one:

 

//Desired list result should be {"cat", "dog", "", "", "", "","mouse", ""}

Names Default to Here(1); str = "cat,dog,,,,mouse,"; lis = Words(str, ","); //This returns {"cat", "dog", "mouse"}

I tried to format the string ahead of time using Substitute:

 

 

newstr = Substitute(str, ",,", ", ,");  //Returns "cat,dog, ,, ,mouse,"  
lis = Words(newstr, ","); //Returns {"cat", "dog", " ", " ", "mouse"}.
finalis = Substitute(lis, " ", "");    //Returns {"cat", "dog", "", "", "mouse"}

Note that there are still 2 consecutive delimiters between dog and mouse after the first Substitute() command.  Thus, following the Words() function, one of the empty items between dog and mouse is still missing.  Also note that the empty item at the end of the string is missing from the list.

 

Does anyone know of a way to do this, preferably one that does not involve a for loop?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Jeff_Perkinson
Community Manager Community Manager

Re: Make "Words" function respect consecutive limiters?

Check the Items() function. It's just like Words(), but consecutive delimiters are treated as though they have a word between them.

 

 

 

-Jeff

View solution in original post

4 REPLIES 4
Highlighted
Jeff_Perkinson
Community Manager Community Manager

Re: Make "Words" function respect consecutive limiters?

Check the Items() function. It's just like Words(), but consecutive delimiters are treated as though they have a word between them.

 

 

 

-Jeff

View solution in original post

Highlighted
txnelson
Super User

Re: Make "Words" function respect consecutive limiters?

Every day, I mean......every day I learn something new about JMP........Jeff.....great response
Jim
Highlighted
nikles
Level IV

Re: Make "Words" function respect consecutive limiters?

Thanks Jeff. Exactly what I'm looking for.
Highlighted
ts2
ts2
Level III

Re: Make "Words" function respect consecutive limiters?

Another way in case you don't have access to Items() function in your version of JMP. 

Note that you will have to choose a delimiter that will not be present in your list items and is not your original delimiter. In this example I am using #.

 

lis = Parse( Substitute( Char( Substitute( Words( Substitute( str, ",", "#,#" ), "," ), "#", "" ) ), "#", "" ) );
[edit] fixed for leading and trailing null list items. 

 

 

Article Labels