4
$\begingroup$

I am importing an excel sheet into Mathematica. The result is a rectangular array. The array contains sequences of commas. Whenever a cell is empty in the Excel sheet consecutive commas appear in the imported array. How to get rid of them? So I am looking for a function $f$ such that for example

f({1,2,3,2,1,,,,6,5,4,3,,,,8})={1,2,3,2,1,6,5,4,3,8}

It doesn't look easy to me because it looks like reinventing the wheel.

Any help will be much appreciated.

$\endgroup$
3
  • 1
    $\begingroup$ I expect that those sequences of commas are actually separating empty strings (""), so you can replace them with e.g. Nothing and they will be spliced away. For instance, {{1, "", 2}, {"", 2, 3}} /. "" -> Nothing gives {{1, 2}, {2, 3}}. $\endgroup$
    – MarcoB
    Commented Mar 1, 2021 at 19:53
  • 1
    $\begingroup$ Or try {1, 2, , , 3} /. Null -> Nothing $\endgroup$
    – LouisB
    Commented Mar 1, 2021 at 20:03
  • $\begingroup$ Thanks, I'll try that right away. $\endgroup$
    – Georgy
    Commented Mar 1, 2021 at 22:21

3 Answers 3

6
$\begingroup$
DeleteCases[{1, 2, 3, 2, 1, , , , 6, 5, 4, 3, , , , 8}, Null]
$\endgroup$
4
$\begingroup$

Remove empty words ("") or Null by replacing by Sequence[]

In[1]:= list = {1,2,3,2,1,,,,6,5,4,3,,,,8};

In[2]:= list /. {Null -> Sequence[], "" -> Sequence[]}

Out[2]= {1,2,3,2,1,6,5,4,3,8}
$\endgroup$
1
$\begingroup$
f[L_List] := Map[If[# === Null, Nothing, #] &, L]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.