Multisplit: Difference between revisions

m
→‎{{header|J}}: clearer result format
(J: simpler)
m (→‎{{header|J}}: clearer result format)
Line 819:
 
=={{header|J}}==
<lang j>multisplit=: {{ NB. x: text, y: separators
'begin sep'=. |:bs=. _,~/:~;(,.&.>i.@#) y I.@E.L:0 x NB.
rlen=. i.2 0 #@>y NB. partially formed result
r=. i.3 0
j=. k=. 0 NB. j indexes text, k indexes of (potential) instances of separators in text
j=. k=. 0
while.j<#x do.
while. j>k{begin do. k=.k+1 end. NB. ignore overlapped separators
'b s'=. k{bs NB. unpackcharacter detailsindex aboutwhere nextseparator appears, separator index
if. _=b do. r,.(j}.x);'';'' return. end. NB. finish if no more separators
txt=. (j + i. b-j){x NB. extract text between separators
j=. b+s{len
j=. b+s{#@>y NB. advance to position after seprator
r=.r,.txt;(s,{::y);b NB. update partial result
end.
}}</lang>
Line 839 ⟶ 840:
Then, loop through the possibilities, skipping over those separators which would overlap with previously used separators.
 
The result consists of twothree rows: The first row is the extracted substrings, the second rowand isthird rows are the "extra credit" part -- for each extracted substring, the numbers in the second row are the separator index for: the following separator (0 for the first separator, 1 for the second, ...), and the locationposition in the original string where the beginning of thethat separator appeared (which is the same as where the end of the extracted substring appeared)started. Note that the very last substring does not have a separator following it, so the extra credit part is blank for that substring.
 
Example use:
 
<lang j> S=: multisplit 'a!===b=';'!=c';'='
┌──┬──┬─┬──┬─┐
S multisplit '==';'!=';'='
│a │b │ │b│ │c│
┌───┬───┬───┬───┬─┐
├──┼──┼─┼──┼─┤
│a │ │b │ │c│
│!=│==│=│!=│ │
├───┼───┼───┼───┼─┤
├──┼──┼─┼──┼─┤
│1 1│0 3│2 6│1 7│ │
│1 │3 │6│7 │ │
└───┴───┴───┴───┴─┘
└──┴──┴─┴──┴─┘
S multisplit '=';'!=';'=='
┌──┬─┬─┬─┬──┬─┐
┌───┬───┬───┬───┬───┬─┐
│a │ │b │b│ │c│
├──┼─┼─┼─┼──┼─┤
├───┼───┼───┼───┼───┼─┤
│!=│=│=│=│!=│ │
│1 1│0 3│0 4│0 6│1 7│ │
├──┼─┼─┼─┼──┼─┤
└───┴───┴───┴───┴───┴─┘
│1 │3│4│6│7 │ │
└──┴─┴─┴─┴──┴─┘
'X123Y' multisplit '1';'12';'123';'23';'3'
┌─┬──┬─┐
┌───┬───┬─┐
│X │ │X│ │Y│
├─┼──┼─┤
├───┼───┼─┤
│0 1│3 2││1│23│
├─┼──┼─┤
└───┴───┴─┘</lang>
│1│2 │ │
└─┴──┴─┘</lang>
 
=={{header|Java}}==
6,951

edits