Split list when there is an empty row
Read carefully documentation of Split
, you need its friend SplitBy
:
SplitBy[a, MemberQ[Null]]
{{{1, 1, 1}, {2, 2, 2}}, {{Null, Null, Null}}, {{3, 3, 3}}}
Here's one way:
Split[{{1, 1, 1}, {2, 2, 2}, {, ,}, {3, 3, 3}},
! (MatchQ[#1, {Null ..}] || MatchQ[#2, {Null ..}]) &]
Here two ways to do it using Split
.
a = {{1, 1, 1}, {2, 2, 2}, {, ,}, {3, 3, 3}, {4, 4, 4}};
With[{p1 = {{_Integer ..} ..}}, Split[a, MatchQ[{#1, #2}, p1] &]
With[{p2 = {{Except[Null] ..} ..}}, Split[a, MatchQ[{#1, #2}, p2] &]]]
Both give
{{{1, 1, 1}, {2, 2, 2}}, {{Null, Null, Null}}, {{3, 3, 3}, {4, 4, 4}}}
The form using pattern p2
is probably better because it is more general. It will handle
c = {{1, 1, 1}, {2, 2, 2}, { , ,}, {x, y, z}, {4, 4, 4}};
correctly, while the form using p1
stumbles on the sub-list {x, y, z}
.
With[{p1 = {{Except[Null] ..} ..}}, Split[c, MatchQ[{#1, #2}, p1] &]]
{{{1, 1, 1}, {2, 2, 2}}, {{Null, Null, Null}}, {{x, y, z}, {4, 4, 4}}}