Function to Strip Out Blank ValuesHi, I'm trying to write a simple JSL function to remove blank values from a list.
Can someone help me tweak this code? (it returns null to me)
thanks, DJ
myList={"apple","orange","banana","","grape","","plum"};
StripBlanks = function({x},
for(a=nitems(x),a>=1,a--,
if(
x[a]=="",
x=remove(x,a)
)
)
);
StripBlanks(myList);
ScriptingRe: Function to Strip Out Blank ValuesYou need to return x as the last statement in your function.
myList = {"apple", "orange", "banana", "", "grape", "", "plum"};
StripBlanks = Function( {x},
For( a = N Items( x ), a >= 1, a--,
If( x[a] == "",
x = Remove( x, a )
)
);
x;
);
StripBlanks( myList );Re: Function to Strip Out Blank ValuesSuper! That was my issue!
Re: Function to Strip Out Blank ValuesGlad that worked for you. I should point out that you don't really need to create a function to do that.
The Loc() function will give you the indices of the blanks in the list and then you can use Remove() or Remove From() to get rid them all at once.
myList = {"apple", "orange", "banana", "", "grape", "", "plum"};
blanks = Loc( mylist, "" ); // [4,6]
blanks = As List( blanks ); // {4, 6}
Remove From( mylist, blanks );
The only trick here is that Loc() returns a matrix and Remove() and Remove From() want a list argument, so you have to recast it As List().