Multisplit: Difference between revisions

J: simpler
(J: simpler)
Line 819:
 
=={{header|J}}==
<lang j>multisplit=: 4{{ NB. x:0 text, y: separators
'sep begin sep'=. |: tbs=. y _,~/:~;(,.&.:(|."1)@;@(>i.@#@[) ,.L:0"0y I.@E.L:0) x
endr=. begini.2 +0 NB. seppartially {formed #@>yresult
j=. k=. 0 NB. j indexes text, k indexes of (potential) instances of separators in text
last=. next=. 0
r=while.j<#x 2 0$0do.
while. next<#j>k{begin do. k=.k+1 end. NB. ignore overlapped separators
'b s'=. k{bs NB. unpack details about next separator
r=. r,.(last}.x{.~next{begin);next{t
if. _=b do. r,.(j}.x);'' return. end. NB. finish if no more separators
last=. next{end
txt=. (j + i. b-j){x NB. extract text between separators
next=. 1 i.~(begin>next{begin)*.begin>:last
j=. b+s{#@>y NB. advance to position after seprator
r=.r,.txt;s,b NB. update partial result
end.
)}}</lang>
r=. r,.'';~last}.x
)</lang>
 
Explanation:
 
First find all potentially relevant separator instances, and sort them in increasing order, by starting location and separator index. <code>sep</code> is separator index, and <code>begin</code> is starting location. <code>end</code> is ending location.
 
Then, loop through the possibilities, skipping over those separators which would overlap with previously used separators.
6,962

edits