Carrot, 77 51 bytes
<th>A^*$v<tr>vl+(^h)*($^F- 1)A"h"S"d"h+(^l)v<table>
(While working on this, I discovered a bug with h
not working and fixed it)
Golfed some bytes by shortening the html as well as using "split, join" instead of "replace"
Try it online!, use the command-line option -d
to see the AST (Note: this uses the new node interpreter, so the older version on the website cannot run this.)
This program takes the input 0-indexed and in reversed order, because of Carrot's weird nature, thus 3 2
printing a 3×4 table.
Run the program like so, ./carrot -f prog.carrot input.txt
Basically creates the header row, then the data rows on another cell of the garden (2D tape), and concatenates them together.
Carrot works on a 2D tape, called a garden. Each cell on the garden is made up of three stack modes, string, float, array. There is a value for each mode, called a "stack" (note: misnomer). These stacks begin empty. When a cell is at a particular mode, the following commands will affect the stack that corresponds to this mode, for example in float mode, the operations will affect the stack float. And of course, there are commands for switching between modes. The modes are important because each operator can be overloaded for each mode and each argument type.
In addition, there are two additional modes (these only affect the commands, not the stack directly), normal mode and caret mode. Normal mode works normally, where there are operators taking in arguments and affecting the stack directly. In caret mode, (almost) every character is interpreted literally as a string, and is later prepended/appended accordingly to the stack. Caret mode is started/ended with carets (append) or down-carets (prepend).
Carrot begins in a cell on the garden, in stack-string mode, and in caret mode.
Beginning in caret-mode, the string <th>A
is added to the initially empty stack-string. Then follows the *
command that duplicates it $
, the input, times. Then <tr>
is prepended to the stack-string by the usage of the down-caret v
. This creates the header row of the table.
To create the data rows, we duplicate the header to another cell. l
moves the IP to the right empty cell, and +
appends (^h)
the string in the cell to the left (essentially copying it to the cell on the right). ()
starts a subshell, a new Carrot program with almost the same tape, and ^
exits out of caret-mode so that we can h
get the string in the left cell. This is then *
duplicated by ($^F- 1)
, the next input minus 1, times.
Still in the right cell, A
sets the array of this cell to its stack- tring split by "h"
. S
joins the stack array by "d"
and sets the stack string to this value. A"h"S"d"
really just replaces h
s with d
s to form the data rows. Now h
we move to the left starting cell.
Now we append the stack string of the cell to the right to this cell using +(^l)
. All that's remaining is to add the <table>
tag, so we do this by v
prepending it.