Multisplit: Difference between revisions
Content added Content deleted
No edit summary |
(J) |
||
Line 4: | Line 4: | ||
Note: Sub - substring, SepNum - separator number in input list, SepPos - separator position in input string.<br> |
Note: Sub - substring, SepNum - separator number in input list, SepPos - separator position in input string.<br> |
||
Input order of separators is important: they are considered in that order. |
Input order of separators is important: they are considered in that order. |
||
=={{header|J}}== |
|||
<lang j>multisplit=:4 :0 |
|||
'sep begin'=.|:t=. y /:~&.:(|."1)@;@(i.@#@[ ,.L:0"0 I.@E.L:0) x |
|||
end=. begin + sep { #@>y |
|||
last=.next=.0 |
|||
r=.2 0$0 |
|||
while.next<#begin do. |
|||
r=.r,.(last}.x{.~next{begin);next{t |
|||
last=.next{end |
|||
next=.1 i.~(begin>next{begin)*.begin>:last |
|||
end. |
|||
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 which conflict with the currently selected sequence. |
|||
Example use: |
|||
<lang j> S multisplit '==';'!=';'=' |
|||
┌───┬───┬───┬───┬─┐ |
|||
│a │ │b │ │c│ |
|||
├───┼───┼───┼───┼─┤ |
|||
│1 1│0 3│2 6│1 7│ │ |
|||
└───┴───┴───┴───┴─┘ |
|||
S multisplit '=';'!=';'==' |
|||
┌───┬───┬───┬───┬───┬─┐ |
|||
│a │ │ │b │ │c│ |
|||
├───┼───┼───┼───┼───┼─┤ |
|||
│1 1│0 3│0 4│0 6│1 7│ │ |
|||
└───┴───┴───┴───┴───┴─┘ |
|||
'X123Y' multisplit '1';'12';'123';'23';'3' |
|||
┌───┬───┬─┐ |
|||
│X │ │Y│ |
|||
├───┼───┼─┤ |
|||
│0 1│3 2│ │ |
|||
└───┴───┴─┘</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |