cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
lakepotter
Level II

Confusion using word( ) function and specified delimiter

Hello all, 

 

I'm trying to manipulate my column names. It's very easy to do once or twice with Find/Replace but I'd like to script it. 

 

Essentially, I want to change a column name from "Singlets/Live leukocytes/Lymphocytes/B cells/A+B+C-,Freq. of Parent" to "B cells/A+B+C-,Freq. of Parent". 

 

Here is my current code: 

 

dt = Current Data Table ();

colList = dt << get column names(string);

For ( i = 1, i <= N Items(colList), i++, 
	If (
		contains(colList[i], "Singlets/Live leukocytes/Lymphocytes/"), 
Column(dt, colList[i]) << set name (Word(2, colList[i], "Lymphocytes/")) ));

 

 

If I run parts of this code individually, here's what I get:

 

line of code:

result in script log

 

 

colList[i]:
"Singlets/Live leukocytes/Lymphocytes/B cells/A+B+C-,Freq. of Parent"


contains(colList[i], "Singlets/Live leukocytes/Lymphocytes/"):
1

Word(2, colList[i], "Lymphocytes/"):
"iv"

Word(3, colList[i], "Lymphocytes/"):
" l"

Word(4, colList[i], "Lymphocytes/"):
"uk"

Word(9, colList[i], "Lymphocytes/"):
"f Par"

 

 

Clearly, my delimiter isn't working the way I expect it to. Any ideas? 

 

 

I originally tried to do Words(4, colList[i], "/") but that only gave me "B cells" since there is another / immediately after that. Unlike other programming languages, I don't know if there is a way to specify '4th word and then everything afterwards until the end' in JSL. I don't think this is possible so I changed the delimiter, thinking that would solve my problem. 

 

Thanks so much for any advice!! 

 

8 REPLIES 8

Re: Confusion using word( ) function and specified delimiter

Use "/" for the delimiter. Also, use the Words() function instead. You will get a list. You can then concatenate list items. In this case the new string will be list[5] || list[5].

 

Pretty easy, eh?

lakepotter
Level II

Re: Confusion using word( ) function and specified delimiter

What if I have an unequal number of list items? This one would be list[4]||" "||list[5] but if I have 6 items in my list and I want it to go to the end... 

 

I'm trying to pseudocode some for loops that go over N items but haven't gotten it yet. 

Re: Confusion using word( ) function and specified delimiter

Use N Items( list ) to determine how many levels exist and take the ones from the end.

lakepotter
Level II

Re: Confusion using word( ) function and specified delimiter

Also, do you know why the original code doesn't work? 

Re: Confusion using word( ) function and specified delimiter

The delimiter is a character, not a character string.

 

You might also find regular expressions useful in this problem.

Re: Confusion using word( ) function and specified delimiter

See Help > Scripting Index > Functions > Character > Words

pmroz
Super User

Re: Confusion using word( ) function and specified delimiter

One confusion is that the delimiter argument to the WORDS function means split this text if you find ANY of these characters.

This approach may get you closer to what you want:

 
collist = {};
collist[1] = "Singlets/Live leukocytes/Lymphocytes/B cells/A+B+C-,Freq. of Parent";
collist[2] = "Singlets/Live leukocytes/Lymphocytes/Another Text";
collist[3] = "Singlets/Live leukocytes/Lymphocytes/Hello World";
remove_text = "Singlets/Live leukocytes/Lymphocytes/";
len_text    = length(remove_text);
for (i = 1, i <= nitems(collist), i++,
	start_pos = contains(colList[i], remove_text);
	if (start_pos,
		end_pos = start_pos + len_text;
		new_name = substr(collist[i], end_pos);
		show(new_name);
		Column(dt, colList[i]) << set name (new_name);
	);
);

 

lakepotter
Level II

Re: Confusion using word( ) function and specified delimiter

OH! Okay, that also makes one of the examples in the scripting guide make MUCH more sense!!

 

I ended up using substr to my advantage! I'll post code after lunch!