Why? Splitting on comma would normally also split 98,00, 12,000,000 and ,-, in two/three pieces. But because they are within the I input character, we ignore ignore the split here.

Challenge rules:

You can assume there will always be an even amount of I characters in the input-string.

You can assume the character I will always have a D next to it (except when it's the first or last character of the input) which can still be properly split. So you won't have something like D = ','; I = '"'; S = 'a,b"c,d"e,f', nor anything like this D=','; I='"'; S='a",b,"c'.

The input-string S could contain none of either D or I. If it contains no D, we output a list with the entire input-string as only item.

The output list won't contain the character I anymore, even when it contained no D (as you can see at the "Abc " becoming 'Abc ' in the example above).

It is possible that the substring within I contains only D. For example: D = ','; I = '"'; S = 'a,",",b,"c","d,e,,",f' would result in ['a', ',', 'b', 'c', 'd,e,,', 'f'].

You can assume that D will never be at the start or end of S, so you won't have to deal with trailing / leading empty items.

When an input has two adjacent D, we'll have an empty item. I.e. D = ','; I = '"'; S = 'a,"b,c",d,,e,"",f' would result in ['a', 'b,c', 'd', '', 'e', '', 'f'].

You can assume the inputs and outputs will only contain printable ASCII in the range [32, 126] (so excluding tabs and newlines).

You are also allowed to output all items new-line delimited instead of returning/outputting a list/array (especially for those languages that don't have lists/arrays; i.e. Retina).

You are allowed to output the list in reversed order if it saves bytes. You are not allowed to output it in sorted or 'shuffled' order, though. So D = ','; I = 'n'; S = 'a,2,b,3,c' can be output as [a,2,b,3,c] or [c,3,b,2,a], but not as [2,3,a,b,c,] or [a,3,b,c,2] for example.

General rules:

This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.

Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.

\$\begingroup\$Most (or all?) of the current answers do not have empty items at the beginning and end of test case 6. Is this a special case, or an error in the testcases? ("", "'ll remove all ", "")\$\endgroup\$
– TFeldOct 29 '18 at 15:47

\$\begingroup\$@TFeld It's a special case. Almost all answers also fail for D = ','; I = '"'; S = ',"a,b",c,,d,""' I just noticed. I'll modify the challenge a bit so empty items won't be possible at the start or end. Based on experience in both Java and 05AB1E in the past, I know how frustrating empty items after split can be to fix manually when the language doesn't do it correctly by default. A test cases like this should still be supported: D = ','; I = '"'; S = 'a,"b,c",d,,e → ['a', 'b,c', 'd', '', 'e'] with an empty item in between.\$\endgroup\$
– Kevin CruijssenOct 29 '18 at 15:57

\$\begingroup\$Is it required that the final output maintain the order of the input? e.g. '1,"2,3"' -> ['2,3','1']\$\endgroup\$
– Kamil DrakariOct 29 '18 at 17:40

1

\$\begingroup\$@KamilDrakari Hmm, I'll add a rule to allow it both forward and backward, but not shuffled. So [a,b,c] and [c,b,a] are allowed outputs, but [a,c,b] or [b,a,c] not for example.\$\endgroup\$
– Kevin CruijssenOct 29 '18 at 17:48

\$\begingroup\$What should be the result of D=','; I='"'; S='a",b,"c' or is it even a valid input?\$\endgroup\$
– ZgarbOct 30 '18 at 9:38

12 Answers
12

Basically the same strategy as DLosc's newer Pip answer, sets aside the "quoted" sections then replaces the delimiter with a newline in the rest of the string, resulting in a newline delimited output

Full explanation:

qV Split on I
m Ã For each segment:
Ïu ? If the segment has an odd index (quoted)...
X Keep it as-is
: Otherwise:
XrWR Replace each D in the segment with a newline
q Join it all to a single string

Note that even if the first item is quoted, it will still end up at index 1 not index 0. If q finds a delimiter as the first character, it makes the first item in the split (index 0) an empty string, so the contents of the quote correctly become the second item (index 1). Here is a demo of correctly dealing with a leading quote.

\$\begingroup\$I just saved some bytes by replacing D with newline outside the quotes and splitting on newline, thereby removing the need to replace newline with D at the end. Could that shorten your code too?\$\endgroup\$
– DLoscOct 30 '18 at 4:00

\$\begingroup\$@DLosc Indeed, thanks for the suggestion! Putting in a new version now\$\endgroup\$
– Kamil DrakariOct 30 '18 at 13:33

\$\begingroup\$@KevinCruijssen Can I submit scan for 4 bytes and then call it with the arguments in the appropriate places?\$\endgroup\$
– J.DoeOct 29 '18 at 15:16

1

\$\begingroup\$@J.Doe Umm.. I dunno. Not too familiar with the possible acceptable input formats for R tbh. As far as I know using a full program with program arguments or STDIN, as well as a (lambda) function with the appropriate parameters or also using STDIN are acceptable by default. In the meta post I linked earlier all valid input-formats (with a positive score) are mentioned. How this works for R I don't know.\$\endgroup\$
– Kevin CruijssenOct 29 '18 at 15:37

\$\begingroup\$@J.Doe your submission right now is a full program, and it currently does not take input from stdin. The shortest solution would be to wrap it into a function.\$\endgroup\$
– GiuseppeOct 29 '18 at 15:41

\$\begingroup\$Your C answer works great, but your JS answer seems to still split on D within I as well as contains I in the output, which can also be seen in the first test cases in your TIO link. (PS: It might also be better to post them as separated answers, with a link stating it's a port of your C answer.)\$\endgroup\$
– Kevin CruijssenOct 29 '18 at 16:04

1

\$\begingroup\$@KevinCruijssen Fixed. Usually I post similar answers together, and only say it's a port if it's from others' work\$\endgroup\$
– l4m2Oct 29 '18 at 16:06

Completely different approach: process the string one character at a time and output the desired results newline-delimited.

How?

a,b,c are cmdline args (respectively representing D,I,S); o is 1;
n is newline (implicit)
We use o for a flag indicating whether or not to change D into newline
Fxc For each character x in c:
xQb? If x equals b (the ignore character),
!:o Logically negate o in-place
O Else, output the following (with no trailing newline):
o? If o is truthy,
xRan x, with a (the delimiter) replaced with newline
x Else, x unchanged

Look ahead to find the values of D and I on the following two lines. Then, if we find an I then eat it and match the characters to the next I and D, otherwise just match characters to the next D or the end of the line.

L$`
$4$5

List captures 4 and 5 from each match; 4 is the capture between two Is while 5 is the capture between two Ds.

Suppose that D =',' and I ='"'. Then the pattern (I ARB . OUTPUT I | ARB . OUTPUT) (D | RPOS(0)) matches strings that look like ".*" or .* followed either by , or by the end of the string, and OUTPUTs the arbitrary (.*) characters, setting the unmatched REMainder to S and iterating so long as S is not empty.

\$\begingroup\$I think list output (as opposed to returning a list) needs to be delimited somehow, so you can tell 1) that it's a list at all, and 2) where one item ends and another begins. (I don't have a specific meta post to back that up, but there is a certain relatedness to the discussion on non-observable behavior. Right now, it's not observable that your program outputs a list rather than, say, a string.)\$\endgroup\$
– DLoscOct 30 '18 at 4:11

\$\begingroup\$So I guess I can replace the tighten with a join by newlines (which will return a list of lists of characters mixed with characters yet as a full program will print the items on new lines).\$\endgroup\$
– Jonathan AllanOct 30 '18 at 7:22

\$\begingroup\$I believe that rearranging input is allowed, as long as you specify the order. To make it clearer, you can use the variables $S, $D and $I with a cost of 0 bytes, for this 50 byte long version.\$\endgroup\$
– Ismael MiguelOct 30 '18 at 9:13

\$\begingroup\$Also, the 50 bytes version, with the $S, $D, $I variables could serve as a guide for the order of the arguments.\$\endgroup\$
– Ismael MiguelOct 30 '18 at 16:26

Your Answer

If this is an answer to a challenge…

…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.

…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

…Please make sure to answer the question and provide sufficient detail.

…Avoid asking for help, clarification or responding to other answers (use comments instead).