| Bytes | Lang | Time | Link |
|---|---|---|---|
| 2220 | 05AB1E | 240527T105804Z | Kevin Cr |
| 061 | APL+WIN | 240526T113114Z | Graham |
| 031 | sed | 240525T204337Z | Jan Blum |
| 068 | Rust | 240524T122554Z | Sylveste |
| 020 | Charcoal | 240525T072201Z | Neil |
| nan | Vyxal | 240524T182728Z | pacman25 |
| 059 | Python 3 | 240524T120239Z | Sylveste |
| 8984 | Google Sheets | 240524T132343Z | z.. |
| 102 | Go | 240524T181728Z | bigyihsu |
| 075 | AWK | 240524T175448Z | C K |
| 035 | Perl 5 p0 | 240524T152641Z | Xcali |
| 051 | JavaScript ES6 | 240524T121103Z | Arnauld |
| 058 | JavaScript Node.js | 240524T111834Z | corvus_1 |
| 025 | Retina | 240524T111430Z | corvus_1 |
05AB1E, 22 (or 20†) bytes
|',„||:'-.ý'}ª'|ì„{|š»
Input-lines as separated inputs.
†: If we're allowed to use a list of lines as I/O, it could be -2 bytes by removing the leading | and trailing »:
Try it online.
Explanation:
| # Get all inputs as a list of strings
',„||: '# Replace all "," with "||" in each string
'-.ý '# Intersperse it with "-" delimiter
'}ª '# Append "}" to the list
'|ì '# Prepend "|" in front of each inner string
„{|š # Prepend "{|" to the list
» # Join the list with newline delimiter
# (after which the result is output implicitly)
APL+WIN, 61 bytes.
Prompts for CSV as a nested matrix of rows:
m←⎕⋄((∊,m=',')/∊,m)←⊂'||'⋄⊃(⊂'{|'),(¯1↓,('|',¨m),⊂'|-'),⊂'|}'
Rust, 73 bytes 68 bytes
- Reduced to 68 bytes thanks to corvus_192
|s|format!("{{|
|")+&s.replace(",","||").replace("
","
|-
|")+"
|}";
Charcoal, 20 bytes
{|¶WS⁺|⟦⪫⪪ι,×|²-⟧M↗}
Try it online! Link is to verbose version of code. Takes input as a list of newline-terminated strings. Explanation:
{|¶
Output the table start.
WS
Loop over each row.
⁺|⟦⪫⪪ι,×|²-⟧
Replace the commas in the row with ||, then print both the row and a -, but prefixing a | to each. (×|² is shorter than || because it doesn't need to be separated from the adjacent strings.)
M↗}
Change the last table row into a table end.
17 bytes to print using the alternative cell format:
{|¶WS⁺|⊞O⪪ι,¦-M↗}
Try it online! Link is to verbose version of code. Takes input as a list of newline-terminated strings. Explanation: As above except each row is split on commas and a - is appended to the list before each element is prefixed with |.
Vyxal, 149 bitsv2, 18.625 bytes
⌐ƛ\|dj\-M;fṪ¤ø.\|vp⁋øḃ
Bitstring:
00011000001001100101010010010000100010000101001011101000010010010110110010001010110101010011111010101100011110000111011001000010001001111011110011110
-1.5 bytes from doing the dash separator inside the map function
⌐ƛ\|dj;:L‹\-ẋY¤ø.\|vp⁋øḃ
⌐ # split strings on commas
ƛ ; # for each row
j # join on the string
\|d # "||"
ẋ # repeat the string...
\- # "-"
:L‹ # rows-1 times
Y # interleave with separated list
¤ø. # surround with empty string
\|vp # prepend a \| to each row
⁋øḃ # join list on newlines and add curly braces to ends
💎
Created with the help of Luminespire.
Python 3, 65 bytes 59 bytes
- Removed 6 bytes thanks to corvus_192
- Fixed an error as pointed out by bigyihsuan on my other answer where I forgot the
|in front of each line of data
lambda s:"{|\n|"+s.translate({44:"||",10:"\n|-\n|"})+"\n|}"
Google Sheets, 89 84 bytes
-5 bytes thanks to @doubleunary
="{|
"®EXREPLACE(SUBSTITUTE(SUBSTITUTE(A1,",","||"),"
","
-
"),"(?m)^","|")&"
|}"
Go, 102 bytes
import."strings"
func f(s string)string{R:=ReplaceAll
return`{|
|`+R(R(s,",","||"),`
`,`
|-
|`)+`
|}`}
AWK, 75 bytes
BEGIN{x="{|\n"}{gsub(/,/,"||");x=x (1~NR?"|":"-\n|")$0"\n|"}END{print x"}"}
AWK is pretty clear about concatenating strings, so we're just jamming in the extra formatting. Run it thus, with the csv file of choice:
awk 'BEGIN{x="{|\n"}{gsub(/,/,"||");x=x (1~NR?"|":"-\n|")$0"\n|"}END{print x"}"}' data.csv
JavaScript (ES6), 51 bytes
s=>`{|
|${s.replace(/\W/g,c=>1/c?`
|-
|`:`||`)}
|}`
Commented
with funny colors, courtesy of highlight.js
s => // s = input string
`{|\n|${ // append the leading string "{|\n|"
s.replace( // replace in s:
/\W/g, // because the data fields are guaranteed
// to contain only letters and digits, it
// is safe to use \W to match all commas
// and newline characters at once
c => // given the matched character c ...
1 / c ? // if c = "\n" (which is zero'ish):
`\n|-\n|` // append the row separator
: // else:
`||` // append the column separator
) // end of replace()
}\n|}` // append the trailing string "\n|}"
JavaScript (Node.js), 58 bytes
s=>`{|
|${s.replace(/,/g,'||').replace(/\n/g,`
|-
|`)}
|}`
