Choose Language Hide Translation Bar
Highlighted
djhanson
Level V

Function to Strip Out Blank Values

Hi, 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);

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Jeff_Perkinson
Community Manager Community Manager

Re: Function to Strip Out Blank Values

You 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 );
-Jeff

View solution in original post

3 REPLIES 3
Highlighted
Jeff_Perkinson
Community Manager Community Manager

Re: Function to Strip Out Blank Values

You 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 );
-Jeff

View solution in original post

Highlighted
djhanson
Level V

Re: Function to Strip Out Blank Values

Super!  That was my issue!

Highlighted
Jeff_Perkinson
Community Manager Community Manager

Re: Function to Strip Out Blank Values

Glad 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().

 

-Jeff
Article Labels