Most Preferred Input Format

If I were to pass your program an integer array as input, what format would you prefer?

I understand that this question is primarily opinion-based, but it is also incredibly useful information for those of us who want to write challenges that appeal to the largest number of people.

• What the point. I won't feel bound by this when writing question. Picking a format can advantage some language and disadvantage others so the site should not have a uniform input style. Diversity is good. – dmckee --- ex-moderator kitten Jun 20 '14 at 1:15
• @dmckee I'm going to use the results of this question to format the inputs for my future challenges. I'm going to point - I said point, not force it down their throat - people here when they ask how to format their inputs for their future challenges. Anyone who wants to cater to popular opinion now has the ability to do so, but only because I asked this question. Anyone who wants to be creative or original can do that too. – Rainbolt Jun 20 '14 at 1:53
• @dmckee I agree that the site should not have a uniform input style. I also welcome a list of input styles each with supporting reasons, so anyone who wants to try something different can use these answers for inspiration. I don't think your point invalidates this question. I expect it to be useful both for those aiming for mass appeal and for those aiming to be awkward... I think this site benefits from both. – trichoplax Jun 20 '14 at 11:06
• Why not say you can use any format on this page that has 2 or more votes? – PyRulez Jun 23 '14 at 13:56
• @dmckee It seems to me you have input specification for something like code golf in mind. For KotH's and code challenges that need a controller program which calls the submissions and feeds them input on STDIN, you'd want the pick the format that is most convenient for most people, because you don't want participants to waste time on parsing their input when they want to work on their algorithm. I find this post very useful for cases like that. (And I think this is the context in which Rusher asked the question, too.) – Martin Ender Jun 27 '14 at 18:40
• @PyRulez, because that implies that you can't use one which doesn't, which is excessively restrictive. – Peter Taylor Jun 28 '14 at 13:48
• @Peter Taylor I neither implied nor meant the converse. – PyRulez Jun 29 '14 at 18:10

Comma, Semicolon separated

1,2,3;4,5,6;7,8,9


The logical separation of comma-delimited lists (in the English language) is semicolons. And this format makes it easy (and roughly equal between languages) to parse, split on ; and ,. Additionally, it is easy to add more dimensions: simply add another delimiter.

• Similar to .CSV, but the lack of a trailing semicolon makes it easier to split (no empty element at the end), and ; is one char shorter than \n – Justin Jun 19 '14 at 21:22
• Not in Golfscript. In golfscript, n is two chars shorter than ';'. But then again, golfscript doesn't really need any benefits in this sense. – John Dvorak Jul 1 '14 at 19:17

.CSV

Terminated with a newline.

1,1,1\n2,2,2\n3,3,3\n


You can see the RFC at http://tools.ietf.org/html/rfc4180, but it looks barbaric. There is no "official" standard.

• +1 This is the only format listed right now that has only two delimiter characters. – Geobits Jun 19 '14 at 18:19
• @Geobits, the ones using {} or [] in combination with either , or whitespace can also be considered to have two delimiters. You just have to ignore {[. – Peter Taylor Jun 19 '14 at 18:30
• @PeterTaylor Right, if you strip them out or ignore them, that works. If you want to use a simple split method, that requires more characters either way. I think CSV is the most language-agnostic format listed so far. Others probably are more readily parsed in certain languages, due to being an inbuilt/expected format. – Geobits Jun 19 '14 at 18:39
• +1 for not privileging a particular language, or in general if parsing via some form of eval is forbidden. – Martin Ender Jun 19 '14 at 18:48

JSON

For example, this 3 by 3 array of integers:

[[1,2,3],[4,5,6],[7,8,9]]


See format at http://json.org/

• +1 as a JavaScript user. ;) – Martin Ender Jun 19 '14 at 18:48
• This can be harder to parse for some languages. For example, Befunge would have problems with this. – Justin Jun 19 '14 at 22:24
• Some languages have default parsing for this. For example, in haskell, you use read, and in python you use eval. Single functions are nice. – PyRulez Jun 23 '14 at 13:55
• @PyRulez Haskell won't like you for the "object" part of "javascript object notation", though. – John Dvorak Jul 1 '14 at 19:19
• @JanDvorak Oh yeah. I think there are JSON parsing libraries any way though. Besides, on this site you usually don't have different types of objects on standard input together. – PyRulez Jul 1 '14 at 21:45

Newline-separated

1
2
3
4
5
6
7
8
9


This is the most generic format, and the easiest to parse in any programming language. Not all programming languages can easily split strings on a delimiter, but all of them can easily read a single line of input from stdin. The program just needs to read the number of lines it expects in a loop, and add each number to an array.

This format does not directly support nested arrays. If you want to represent a 2D matrix, add two more lines before the list with the number of rows followed by the number of columns, e.g. 3 and 3.

• +1 for using nothing but newlines. I think that JSON and this answer are basically the two extremes of this spectrum. – Rainbolt Jun 27 '14 at 15:33
• +1 This is the most natural format to read an array/list in sed and other similar languages. – seshoumara Sep 24 '16 at 9:02

Comma-Separated, Curly-Brace Wrapped

For example, this 3 by 3 array of integers:

{{1,1,1},{2,2,2},{3,3,3}}

• +1 as a Mathematica user :) – Martin Ender Jun 19 '14 at 18:46

Having the answer dictate how an array is to be entered would be preferable since some have integrated parsers for certain formats while other don't.

Since it isn't here yet I'll add LISP lists and vectors (called proper arrays in other languages):

(1 2 3 4 5)  ; list
#(1 2 3 4 5) ; vector


You can easily nest them:

((1 2 3) (4 5 6))    ; list in list
#(#(1 2 3) #(4 5 6)) ; vector in vector (compatible)
#2A((1 2 3) (4 5 6)) ; 2 dimentional vector in CL


Space-separated, square-bracket wrapped

[[1 2 3][4 5 6][7 8 9]]


This is the format which GolfScript can evaluate most easily, so naturally GolfScript users will prefer it.

• Per-language preferences aside, I think this is the most readable option. – Dennis Jun 21 '14 at 18:09

Space separated:

1 2 3 4 5 6 7 8 9


which would give the 3x3

1 4 7
2 5 8
3 6 9


(at least it will if you're a column-major programmer).

• With this you also have to explicitly list dimensions, don't you? – Geobits Jun 19 '14 at 18:20
• @Geobits: I would presume that you would know what size integer array is being fed in the first place. – Kyle Kanos Jun 19 '14 at 18:21
• If it's something static like a tic-tac-toe board, you'd know. If it's "evaluate this MxN maze", you wouldn't. – Geobits Jun 19 '14 at 18:23
• @Geobits: Easily taken care of using allocatable... – Kyle Kanos Jun 19 '14 at 18:23
• I'm not sure how allocatable helps here. My point is that if I am given an MxN maze with 36 values in this format, I don't know up-front whether it's 36x1,18x2,12x3,9x4, or 6x6. Every other format listed makes it clear without having to explicitly give the dimensions. – Geobits Jun 19 '14 at 18:29
• @Geobits: I suppose it would require knowing what the shape of the array should be – Kyle Kanos Jun 19 '14 at 18:32
• The question says integer array, not array of arrays or integer matrix. This is the only valid answer I can see so far :) – aditsu quit because SE is EVIL Jun 24 '14 at 19:28
• For two-dimensional arrays, newlines make pretty natural row delimiters, exactly as in your second example. Or, at least, that's what I initially assumed you were proposing. – Ilmari Karonen Jun 25 '14 at 23:22