cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
Shadow
Level I

Using "Left" on a string

Hi,

I think this should be easy but have spent too much time trying to figure it out. I have a more complex worksheet with a longer list (that refers to column names that may change) but what I am trying to do is create a new list to be used else where by removing the word "_Yield" from my original list. Simple example below;

 

<JSL>

Names Default To Here( 1 );
exurl = {"Test_Yield, Help_Yield"};
X = Left(exurl, Contains( exurl, "_Yield" ) - 1 );

 

For this I want to get X = {"Test", "Help"} but I just get X = exurl

 

1 ACCEPTED SOLUTION

Accepted Solutions
SDF1
Super User

Re: Using "Left" on a string

Hi @Shadow ,

 

  Along with what @JohnDMoore1 suggested for the substitute, which is a great way to do it, another option can be the following:

Names Default to Here(1);

exurl = {"Test_Yield", "Help_Yield"};

x={};
For(i=1, i<=NItems(exurl), i++,
	x[i]=left(exurl[i], Contains(exurl[i], "_Yield")-1);
);

  I think one of the things that's throwing the Left command off is that it is a multi-element list -- even though the scripting index says it can deal with lists, it doesn't appear to actually have that ability. The For loop helps to build your output variable as a list. Sometimes this kind of approach is more flexible, especially when you have large data tables and need to rename many columns, etc.

 

  Also, I think there is a mistake in your example url that you gave. You wrote: exurl={"Test_Yield, Help_Yield"}, which is only a single string, not a list. I think what you meant to write is exurl={"Test_Yield", "Help_Yield"} which has the extra quotes and comma separating the elements in the list.

 

Hope this helps!,

DS

View solution in original post

4 REPLIES 4
JohnDMoore1
Level II

Re: Using "Left" on a string

I tend to use substitute in these situations

 

X = substitute(exurl,"_Yield","");
Shadow
Level I

Re: Using "Left" on a string

Hi John, Thanks for your reply. I had tried that but still get X = excurl. 

SDF1
Super User

Re: Using "Left" on a string

Hi @Shadow ,

 

  Along with what @JohnDMoore1 suggested for the substitute, which is a great way to do it, another option can be the following:

Names Default to Here(1);

exurl = {"Test_Yield", "Help_Yield"};

x={};
For(i=1, i<=NItems(exurl), i++,
	x[i]=left(exurl[i], Contains(exurl[i], "_Yield")-1);
);

  I think one of the things that's throwing the Left command off is that it is a multi-element list -- even though the scripting index says it can deal with lists, it doesn't appear to actually have that ability. The For loop helps to build your output variable as a list. Sometimes this kind of approach is more flexible, especially when you have large data tables and need to rename many columns, etc.

 

  Also, I think there is a mistake in your example url that you gave. You wrote: exurl={"Test_Yield, Help_Yield"}, which is only a single string, not a list. I think what you meant to write is exurl={"Test_Yield", "Help_Yield"} which has the extra quotes and comma separating the elements in the list.

 

Hope this helps!,

DS

jthi
Super User

Re: Using "Left" on a string

If you have access to JMP16+, this is a good place to use Transform Each

Names Default To Here(1);
exurl = {"Test_Yield", "Help_Yield"};
X = Transform Each({url}, exurl, Substitute(url, "_Yield", ""));
show(X); // X = {"Test", "Help"};

 

-Jarmo