| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | AWK | 240628T040935Z | 鳴神裁四点一号 |
| nan | 221215T165704Z | bigyihsu | |
| 004 | MATL | 170228T211549Z | Suever |
| 082 | R | 170516T132451Z | Giuseppe |
| 004 | APL Dyalog | 170516T095753Z | Adá |
| 019 | Cheddar | 170301T031726Z | Downgoat |
| 095 | REXX | 170329T074446Z | idrougge |
| nan | GNU sed | 170304T082613Z | seshouma |
| 2223 | Powershell | 170228T200021Z | colsw |
| 033 | Rebol | 170301T202937Z | draegtun |
| nan | Ruby using 'n' | 170301T110716Z | G B |
| 005 | CJam | 170301T102836Z | Linnea G |
| 002 | Jelly | 170228T194515Z | Jonathan |
| 025 | MATLAB / Octave | 170228T205812Z | Suever |
| 011 | Mathematica | 170228T205445Z | Greg Mar |
| 003 | Japt | 170228T201058Z | ETHprodu |
| 026 | JavaScript | 170228T200045Z | fəˈnɛtɪk |
| 033 | Python | 170228T194814Z | Trelzevi |
| 029 | Haskell | 170228T194649Z | nimi |
| 005 | 05AB1E | 170228T194452Z | Okx |
AWK, 31 + 2 = 33 bytes
May this be a cheat, but the delimiter of input must be hard coded to the argument of -F. E.g. -F,, -F;, -F .
i=NF{for(;i;)print NR,i,$(i--)}
Usage
Delimiter is given to the -F option. Your DSV is given from standard input. One line per record. If you want to contain a value with newlines, you need to simulate it with a string of \n. For example if you want to input this kind of CSV:
Haha,"newline
>=)"
You must convert it to:
Haha,newline\n>=)
Output format
It consists of three space-separated lines.
- Row number (1-indexed)
- Column number (1-indexed)
- Value
Each column is shown in reverse order but I think this can be justified because I clarified row and column indices. Example:
1 4 sample
1 3 some
1 2 is
1 1 Here
2 4 commas
2 3 by
2 2 delimited
2 1 data
Explained
# For each line,
# -F? # split line by a delimiter to store to $1, $2, ..., $NF as strings
i=NF{ # assign number of columns
for(;i;) # while i is not zero
print NR,i,$(i--) # print line number, i, value of $i with space delimited. Then decrement i.
}
Go, 136 bytes
import."strings"
func f(S[]string,d rune)(O[][]string){for _,s:=range S{O=append(O,FieldsFunc(s,func(r rune)bool{return r==d}))}
return}
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
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.
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.
≠⊂¨⊢
≠ 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.
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 :(
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.
GNU sed, 48 + 1(r flag) = 49 bytes
1h;1d
:
G
/,$/bp
s:(.)(.*)\n\1:,\2:
t
:p;s:..$::
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)
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
Rebol, 33 bytes
func[b s][map-each n b[split n s]
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 thesplitfunction - I tried to avoid
chompbut any other solution seems to be longer than this.
CJam, 5 bytes
l~l./
Explanation:
l~ e#Input evaluated (as list)
l e#Another line of input
./ e#Split first input by second
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.
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'}, ' ')
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"}}
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
JavaScript, 26 bytes
x=>y=>x.map(n=>n.split(y))
Receives input in format (array of strings)(delimiter)
Python, 33 bytes
lambda a,c:[x.split(c)for x in a]
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"]].
05AB1E, 5 bytes
vy²¡ˆ
Explanation:
v For each element in the input array
y Push the element
² Push second input
¡ Split
ˆ Add to array