How to remove empty elements in a string list?

jospercomp shared this question 2 years ago
Answered

input

list1={"ABC","","","BCA",""","CBA"}

output

list2={"ABC","BCA","CBA"}

Best Answer
photo

KeepIf(¬(T ≟ ""), T, list1)

Comments (3)

photo
2

list1 = {"ABC", "", "", "BCA", "", "CBA"}
RemoveUndefined(Sequence(If(Element(list1, i) ≠ "", Element(list1, i), ?), i, 1, Length(list1)))

photo
1

var n = ggbApplet.getValue("n");
var s = "";
var c = 0;
var str = "";
for (var i = 1; i <= n; i++) {
ggbApplet.evalCommand("v=Element(l2," + i + ")")
s = ggbApplet.getValueString("v");
if (s != null) {
if (c == 0)
str = str + "l3={\"" + s + "\"";
else
str = str + ",\"" + s + "\"";
c++;
}
}
str = str + "}";
ggbApplet.evalCommand(str);

photo
3

KeepIf(¬(T ≟ ""), T, list1)

© 2023 International GeoGebra Institute