12
\$\begingroup\$

Slightly more than an inverse of this.

In: Multi-line DSV data and a single delimiter character. The DSV may be taken as a file, a filename, line-break separated string, list of strings, etc. All records have the same number of fields, and no field is empty. Data does not contain the delimiter character and there is no quoting or escaping mechanism.

Out: A data structure representing the DSV, e.g. a list of lists of strings or a matrix of strings.

Examples

["here is,some,sample","data,delimited,by commas"] and ",":
[["here is","some","sample"],["data","delimited","by commas"]]

["hello;\"","\";world","\";\""] and ";":
[["hello","\""],["\"","world"],["\"","\""]] (escapes because this example uses JSON)

["to be or not","that is the question"] and " ":
[["to","be","or","not"],["that","is","the","question"]]

\$\endgroup\$
  • \$\begingroup\$ So just to clarify, we simply split each item at instances of the given char? \$\endgroup\$ – ETHproductions Feb 28 '17 at 19:42
  • \$\begingroup\$ @ETHproductions That's right. \$\endgroup\$ – Adám Feb 28 '17 at 19:45
  • \$\begingroup\$ How should we split the strings if the first or last character is the delimiter? ",for,example,this,string," \$\endgroup\$ – G B Mar 1 '17 at 10:28
  • \$\begingroup\$ @GB no field is empty \$\endgroup\$ – Adám Mar 1 '17 at 10:35
  • \$\begingroup\$ So we can assume it won't happen? \$\endgroup\$ – G B Mar 1 '17 at 10:42

18 Answers 18

3
\$\begingroup\$

Jelly, 3 2 bytes

Dennis points out that while the 2 byte solution appears to not work, the dyadic link itself does, and that it is actually the way command line arguments are parsed that make it look that way.

ṣ€

Try It Online! - footer calls the function with left and right set explicitly, and formats as a grid*.

Exactly as the below, except splits at occurrences of the right argument rather than at sublists equal to the right argument.


œṣ€

The 3 byter - footer displays the result as a grid*.

A dyadic link (function) that takes the DSV list on the left and the delimiter on the right.

How?

œṣ€ - Main link: list l, delimiter d
  € - for each item in l:
œṣ  -     split at occurrences of sublists equal to d

* As a full program the implicit output would simply "smush" together all the characters, so the footer of the TIO link calls the link as a dyad and uses G to format the result nicely.

\$\endgroup\$
  • \$\begingroup\$ @Okx the implicit output would simply "smush" together all the characters \$\endgroup\$ – Adám Feb 28 '17 at 19:48
  • \$\begingroup\$ @Okx Yes it is a function that returns a list. The footer is to override the implicit output that occurs when it is run as a full program. \$\endgroup\$ – Jonathan Allan Feb 28 '17 at 19:48
7
\$\begingroup\$

Japt, 3 bytes

mqV

Test it online! (Uses the -Q flag to prettyprint the output)

mqV  // Implicit: U, V = inputs
m    // Map each item in U by the following function:
 qV  //   Split the item at instances of V.
     // Implicit: output result of last expression
\$\endgroup\$
  • \$\begingroup\$ :O a JSGL beat MATL! \$\endgroup\$ – Downgoat Mar 1 '17 at 3:18
7
\$\begingroup\$

Powershell, 25 22/23 bytes

Two Options, one just calls split on the first arg, using the second arg as a delim value.

$args[0]-split$args[1]

One byte longer, builtin to parse csvs, takes filename as first arg and delim as second.

ipcsv $args[0] $args[1]

-2 because it doesn't require the -Delimiter (-D) param, and will assume it by default.

sadly powershell cannot pass an array of two params, as it will assume they are both files, and will run the command against it twice, no other two-var input method is shorter than this as far as I can see, so this is likely the shortest possible answer.

ipcsv is an alias for Import-Csv, takes a file name as the first unnamed input, and the delim character as the second by default behavior.

Run against the example from the wiki page returns

PS C:\Users\Connor\Desktop> .\csvparse.ps1 'example.csv' ','

Date     Pupil               Grade
----     -----               -----
25 May   Bloggs, Fred        C
25 May   Doe, Jane           B
15 July  Bloggs, Fred        A
15 April Muniz, Alvin "Hank" A
\$\endgroup\$
6
\$\begingroup\$

Python, 33 bytes

lambda a,c:[x.split(c)for x in a]
\$\endgroup\$
5
\$\begingroup\$

Haskell, 29 bytes

import Data.Lists
map.splitOn

Usage example: (map.splitOn) " " ["to be or not","that is the question"] -> [["to","be","or","not"],["that","is","the","question"]].

\$\endgroup\$
4
\$\begingroup\$

05AB1E, 5 bytes

vy²¡ˆ

Try it online!

Explanation:

v     For each element in the input array
 y    Push the element
  ²   Push second input
   ¡  Split
    ˆ Add to array
\$\endgroup\$
4
\$\begingroup\$

JavaScript, 26 bytes

x=>y=>x.map(n=>n.split(y))

Receives input in format (array of strings)(delimiter)

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Mathematica, 11 bytes

StringSplit

Builtin function taking two arguments, a list of strings and a character (and even more general than that). Example usage:

StringSplit[{"to be or not", "that is the question"}, " "]

yields

{{"to", "be", "or", "not"}, {"that", "is", "the", "question"}}
\$\endgroup\$
4
\$\begingroup\$

MATLAB / Octave, 41 25 bytes

@(x,d)regexp(x,d,'split')

Creates an anonymous function named ans which accepts the first input as a cell array of strings and the second input as a string.

ans({'Hello World', 'How are you'}, ' ')

Try it Online

\$\endgroup\$
4
\$\begingroup\$

Cheddar, 19 bytes

a->b->a=>@.split(b)

nice demonstration of looping abilities. I added new composition and f.op. blocks so that allows for interesting golfing. (=>:@.split) is supposed to work but it doesn't :(

\$\endgroup\$
3
\$\begingroup\$

MATL, 14 12 4 bytes

H&XX

Try it at MATL Online (the link has a modification at the end to show the dimensionality of the output cell array).

Explanation

        % Implicitly grab the first input as a cell array of strings
        % Implicitly grab the delimiter as a string
H       % Push the number literal 2 to the stack
&XX     % Split the input at each appearance of the delimiter
        % Implicitly display the result
\$\endgroup\$
1
\$\begingroup\$

CJam, 5 bytes

l~l./

Explanation:

l~     e#Input evaluated (as list)
  l    e#Another line of input
   ./  e#Split first input by second
\$\endgroup\$
1
\$\begingroup\$

Ruby using '-n', 17+1 = 18 bytes

p chomp.split *$*

How it works

  • Input from file
  • separator is given as command line parameter
  • since we only have 1 parameter, *$* splats the string and we can use it as a parameter for the split function
  • I tried to avoid chomp but any other solution seems to be longer than this.
\$\endgroup\$
1
\$\begingroup\$

Rebol, 33 bytes

func[b s][map-each n b[split n s]
\$\endgroup\$
1
\$\begingroup\$

GNU sed, 48 + 1(r flag) = 49 bytes

1h;1d
:
G
/,$/bp
s:(.)(.*)\n\1:,\2:
t
:p;s:..$::

Try it online!

In sed there are no data types, but a natural representation of a list would be a collection of lines. As such, the input format consists of DSV records each on a separate line, with the delimiter present on the first line.

Explanation: by design, sed runs the script as many times as there are input lines

1h;1d                  # store delimiter, start new cycle
:                      # begin loop
G                      # append saved delimiter
/,$/bp                 # if delimiter is ',', skip replacements and go to printing
s:(.)(.*)\n\1:,\2:     # replace first occurrence of delimiter with ','
t                      # repeat
:p;s:..$::             # print label: delete appended delimiter (implicit printing)
\$\endgroup\$
1
\$\begingroup\$

REXX, 95 bytes

arg f d
do l=1 while lines(f)
    n=linein(f)
    do #=1 while n>''
        parse var n w (d) n
        o.l.#=w
    end
end

Takes a filename and a delimiter as arguments, contents of file are put in stem o.

\$\endgroup\$
  • \$\begingroup\$ Is all that whitespace really necessary? \$\endgroup\$ – Adám Mar 29 '17 at 8:03
  • \$\begingroup\$ No, I only indented it for readability. The byte count is for unindented code. \$\endgroup\$ – idrougge Mar 29 '17 at 9:03
  • \$\begingroup\$ Which flavour of REXX is this? \$\endgroup\$ – Adám Mar 29 '17 at 9:39
  • \$\begingroup\$ I think it's pure ANSI REXX. I've only tested it with Regina. \$\endgroup\$ – idrougge Mar 29 '17 at 11:02
  • 1
    \$\begingroup\$ On its way to TIO... \$\endgroup\$ – Adám Mar 29 '17 at 11:04
0
\$\begingroup\$

APL (Dyalog), 4 bytes

In versions up to and including 15.0, this needs ⎕ML←3 which is default by many. From version 16.0 can just be replaced by for the same effect.

Takes separator as left argument and DSV as right argument.

≠⊂¨⊢

Try it online!

 the inequalities (of the left argument and the right argument)

⊂¨ partition each

 right argument

By partition is mean to remove all elements indicated by a corresponding zero in the left argument, and begin a new partition whenever a the corresponding number in the left argument is greater than its predecessor, i.e. on every one if the left argument is Boolean, as is the case here.

\$\endgroup\$
0
\$\begingroup\$

R, 8 bytes (2 ways)

R has two builtin functions that meet the requirements of this challenge:

strsplit

takes a vector of strings and a separator, and returns a list of vectors of the separated strings.

read.csv

takes a file name and a separator, and returns a data frame. Technically this might be 10 bytes because it needs the option header=F so it won't read the first elements as the column names. Currently the TIO link reads from stdin.

Try these online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.