If you are requesting technical assistance with Twine, please specify:
Twine Version: 2.3.13
Story Format: Sugarcube 2.34.1
Hi everyone. I’m having trouble getting an array - $Record - to print without commas. I would like it to print each record on a new line (working) but each line with adjustable spaces if possible (for adding to a table).
Below is a sample of the current code (printing with commas)
<<set $Day =1>><<set $Sector="Farming">><<set $ShareBnumber=1>><<set $Worth=10>>
<<set $Transaction = [$Day, $Sector, $ShareBnumber, $Worth]>>
<<set $Record to >><<run $Record.push($Transaction)>>
<<set $Day =2>><<set $Sector="Mining">><<set $ShareBnumber=4>><<set $Worth=20>>
<<set $Transaction =[$Day,$Sector,$ShareBnumber,$Worth]>>
<<= $Record.join(' <br> ')>>
The current output is:
I would like it to look like (Table headings pre-prepared):
Day Sector Number Value
1 Farming 1 $10
2 Mining 4 $20
I have tried a number of things with no success so any assistance would be greatly appreciated.
As far as I know when you print an array in sugarcube, it’s always written in the form you’ve met, with commas between each members of the array.
However you can write an individual member of an array. For instance your array $Record is currently two arrays.$Record, itself an array consisting of 1,“Farming”,1 and 10, and $Record, again an array, consisting of 2,“Mining”,4 and 20.
As writing in each cell of your table the exact member of $Record will be both boresome and prone to provoke errors in the long run, here’s a code that should allow to build your table:
<<for _i to 0;_i<$Record.length;_i++>><tr><<for _ii to 0;_ii<$Transaction.length;_ii++>><td><<if _ii<$Transaction.length-1>>$Record[_i][_ii]<<else>><<print "$"+$Record[_i][_ii]>><</if>></td><</for>></tr><</for>></table>
The first part of the code creates the title row, then the second part runs two nestled loops, one for each line, and one for each cell of the line. I’ve added a a test for the monetary value to add ‘$’ before the value, it will allow to keep the member of the array as a numeric variable.
I will give this a go.
Thanks so much souppilouliouma
Just letting you know this works very well and looks much better than what I had.