Create an HTML table: Difference between revisions
m
→{{header|EasyLang}}
m (→{{header|Factor}}: change output style) |
|||
(32 intermediate revisions by 19 users not shown) | |||
Line 7:
* The numbers should be aligned in the same fashion for all columns.
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">UInt32 seed = 0
F nonrandom(n)
:seed = 1664525 * :seed + 1013904223
R Int(:seed >> 16) % n
F rand9999()
R nonrandom(9000) + 1000
F tag(tag, txt, attr = ‘’)
R ‘<’tag‘’attr‘>’txt‘</’tag‘>’
V header = tag(‘tr’, ‘,X,Y,Z’.split(‘,’).map(txt -> tag(‘th’, txt)).join(‘’))"\n"
V rows = (1..5).map(i -> tag(‘tr’, tag(‘td’, i, ‘ style="font-weight: bold;"’)‘’(0.<3).map(j -> tag(‘td’, rand9999())).join(‘’))).join("\n")
V table = tag(‘table’, "\n"header‘’rows"\n")
print(table)</syntaxhighlight>
{{out}}
<pre>
<table>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td style="font-weight: bold;">1</td><td>7470</td><td>1256</td><td>9708</td></tr>
<tr><td style="font-weight: bold;">2</td><td>8769</td><td>8170</td><td>5750</td></tr>
<tr><td style="font-weight: bold;">3</td><td>5526</td><td>6945</td><td>7277</td></tr>
<tr><td style="font-weight: bold;">4</td><td>3128</td><td>8214</td><td>4337</td></tr>
<tr><td style="font-weight: bold;">5</td><td>5227</td><td>5006</td><td>1442</td></tr>
</table>
</pre>
=={{header|360 Assembly}}==
{{trans|BBC BASIC}}
<
CREHTML CSECT
USING CREHTML,R13
Line 72 ⟶ 103:
XDEC DS CL12
YREGS
END CREHTML</
{{out}}
<pre>
Line 132 ⟶ 163:
</tr>
</table>
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE ROW_COUNT="4"
DEFINE COL_COUNT="3"
PROC Main()
CHAR ARRAY headers=[0 'X 'Y 'Z]
BYTE row,col
INT v
PrintE("<html>")
PrintE("<head></head>")
PrintE("<body>")
PrintE("<table border=1>")
PrintE("<thead align=""center"">")
Print("<tr><th></th>")
FOR col=1 TO COL_COUNT
DO
PrintF("<th>%C</th>",headers(col))
OD
PrintE("</tr>")
PrintE("</thead>")
PrintE("<tbody align=""right"">")
FOR row=1 TO ROW_COUNT
DO
PrintF("<tr><th>%B</th>",row)
FOR col=1 TO COL_COUNT
DO
v=800+Rand(0)*5
PrintF("<td>%I</td>",v)
OD
PrintE("</tr>")
OD
PrintE("</tbody>")
PrintE("</table>")
PrintE("</body>")
PrintE("</html>")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Create_an_HTML_table.png Screenshot from Atari 8-bit computer]
<pre>
<html>
<head></head>
<body>
<table border=1>
<thead align="center">
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
</thead>
<tbody align="right">
<tr><th>1</th><td>1405</td><td>1335</td><td>1725</td></tr>
<tr><th>2</th><td>890</td><td>1960</td><td>1980</td></tr>
<tr><th>3</th><td>1005</td><td>835</td><td>1125</td></tr>
<tr><th>4</th><td>1180</td><td>1095</td><td>1155</td></tr>
</tbody>
</table>
</body>
</html>
</pre>
=={{header|Ada}}==
Line 137 ⟶ 228:
We define a generic package to output HTML tables:
<
generic
Line 155 ⟶ 246:
procedure Print(Items: Item_Array; Column_Heads: Header_Array);
end HTML_Table;</
The implementation of the package:
<
procedure Print(Items: Item_Array; Column_Heads: Header_Array) is
Line 214 ⟶ 305:
end Print;
end HTML_Table;</
Here is the main program, using an instance of HTML_Table:
<
procedure Test_HTML_Table is
Line 249 ⟶ 340:
T.Print(Items => The_Table,
Column_Heads => (T.Convert("X"), T.Convert("Y"), T.Convert("Z")));
end Test_HTML_Table;</
Each time you run the program, you get different random values for the table. {{out}}
<
<thead align = "right">
<tr><th></th><td>X</td><td>Y</td><td>Z</td></tr>
Line 263 ⟶ 354:
<tr><td> 4</td><td> 31</td><td> 6897</td><td> 8265</td></tr>
</tbody>
</table></
{{out}}
Line 276 ⟶ 367:
{{trans|ALGOL 68}}
Tested with Agena 2.9.5 Win32
<
numberedLeft := 1; # " " " " " " "
numberedRight := 2; # " " " " " " "
Line 330 ⟶ 421:
t.data := [ [ 1001, 1002, 1003 ], [ 21, 22, 23 ], [ 201, 202, 203 ] ];
writeHtmlTable( io.stdout, t )
epocs</
{{out}}
<table cellspacing='0' colspacing='0' border='1'>
Line 360 ⟶ 451:
=={{header|ALGOL 68}}==
<
INT numbered left = 1; # " " " " " " #
INT numbered right = 2; # " " " " " " #
Line 437 ⟶ 528:
headings OF t := ( "A", "B", "C" );
data OF t := ( ( 1001, 1002, 1003 ), ( 21, 22, 23 ), ( 201, 202, 203 ) );
write html table( stand out, t )</
{{out}}
<table cellspacing="0" colspacing="0" border="1">
Line 466 ⟶ 557:
</table>
=={{header|Applesoft BASIC}}==
{{trans|BBC BASIC}}
Untokenized, this program is only about a dozen bytes shorter than the HTML that it outputs.
<syntaxhighlight lang="gwbasic"> 0 NCOLS = 3:NROWS = 4
1 PRINT "<HTML><HEAD></HEAD><BODY>"
2 PRINT "<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0>"
3 FOR ROW = 0 TO NROWS
40 PRINT " <TR><TH>";
50 IF ROW THEN PRINT ROW;
60 PRINT "</TH>"
70 FOR COL = 1 TO NCOLS
800 PRINT MID$ (" ",1,(COL = 1 OR ROW) * 99) MID$ ("<TH>" + CHR$ (87 + COL) + "</TH>",1,(ROW = 0) * 99) MID$ ("<TD ALIGN=RIGHT>" + STR$ ( INT ( RND (1) * 10000)) + "</TD>",1,(ROW > 0) * 99);
900 PRINT MID$ ("</TR>",1,(COLS = NCOLS) * 99) MID$ ( CHR$ (13),1,ROW OR COL = NCOLS);
10000 NEXT COL,ROW: PRINT "</TABLE></BODY></HTML>";</syntaxhighlight>
{{out}}
<pre>
<HTML><HEAD></HEAD><BODY>
<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0>
<TR><TH></TH>
<TH>X</TH><TH>Y</TH><TH>Z</TH></TR>
<TR><TH>1</TH>
<TD ALIGN=RIGHT>3326</TD>
<TD ALIGN=RIGHT>7702</TD>
<TD ALIGN=RIGHT>5883</TD></TR>
<TR><TH>2</TH>
<TD ALIGN=RIGHT>832</TD>
<TD ALIGN=RIGHT>9821</TD>
<TD ALIGN=RIGHT>1212</TD></TR>
<TR><TH>3</TH>
<TD ALIGN=RIGHT>9054</TD>
<TD ALIGN=RIGHT>2895</TD>
<TD ALIGN=RIGHT>2420</TD></TR>
<TR><TH>4</TH>
<TD ALIGN=RIGHT>3026</TD>
<TD ALIGN=RIGHT>4862</TD>
<TD ALIGN=RIGHT>1416</TD></TR>
</TABLE></BODY></HTML>
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">table: function [content]
-> join @["<table>" join @content "</table>"]
header: function [cells] -> join @["<tr>" join @cells "</tr>"]
th: function [lbl] -> join @["<th>" lbl "</th>"]
row: function [no]
-> join @[
"<tr><td style='font-weight:bold'>" no "</td>"
"<td>" random 1000 9999 "</td>"
"<td>" random 1000 9999 "</td>"
"<td>" random 1000 9999 "</td></tr>"
]
print table [
header [th"" th"X" th"Y" th"Z"]
row 1
row 2
row 3
row 4
row 5
]</syntaxhighlight>
{{out}}
<table><tr><th></th><th>X</th><th>Y</th><th>Z</th></tr><tr><td style='font-weight:bold'>1</td><td>6907</td><td>9698</td><td>1033</td></tr><tr><td style='font-weight:bold'>2</td><td>3406</td><td>9857</td><td>5047</td></tr><tr><td style='font-weight:bold'>3</td><td>6638</td><td>6668</td><td>4313</td></tr><tr><td style='font-weight:bold'>4</td><td>3797</td><td>7335</td><td>7819</td></tr><tr><td style='font-weight:bold'>5</td><td>4780</td><td>5231</td><td>1661</td></tr></table>
=={{header|AutoHotkey}}==
{{trans|C}}
<
Loop 4
out .= "`r`n<tr><th>" A_Index "</th><td>" Rand() "</td><td>" Rand() "</td><td>" Rand() "</tr>"
Line 570 ⟶ 634:
Random, n, 1, % u
return n
}</
{{out}}
<table style="text-align:center; border: 1px solid"><th></th><th>X</th><th>Y</th><th>Z</th><tr>
Line 581 ⟶ 645:
=={{header|AWK}}==
<
BEGIN {
print "<table>\n <thead align = \"right\">"
Line 589 ⟶ 653:
}
print " </tbody>\n</table>\n"
}</
{{out}}
<pre><table>
Line 612 ⟶ 676:
=={{header|Batch File}}==
This will overwrite any file by the name of "table.html" in the current directory.
<
@echo off
setlocal enabledelayedexpansion
Line 652 ⟶ 716:
) > table.html
start table.html
</syntaxhighlight>
{{out}}
<pre>
Line 717 ⟶ 781:
{{works with|BBC BASIC for Windows}}
Uses BBC BASIC's *spool command to create a file.
<
nrows% = 4
Line 747 ⟶ 811:
SYS "ShellExecute", @hwnd%, 0, "temp.htm", 0, 0, 1
</syntaxhighlight>
{{out}}
<pre><html><head></head><body>
Line 765 ⟶ 829:
[[File:HTMLtable_BBC.gif]]
=={{header|BQN}}==
Returns a simple string.
<code>Tag</code> is a helper function which encloses the left argument in the right argument tag type.
<syntaxhighlight lang="bqn">Tag ← {∾"<"‿𝕨‿">"‿𝕩‿"</"‿𝕨‿">"}
•Show "table" Tag ∾∾⟜(@+10)¨("tr"Tag∾)¨⟨"th"⊸Tag¨ ""<⊸∾⥊¨"XYZ"⟩∾("td" Tag •Fmt)¨¨ 0‿0‿1‿2 + 1‿3‿3‿3 × 4/⟨↕4⟩</syntaxhighlight><syntaxhighlight lang="bqn">"<table><tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>0</td><td>1</td><td>2</td><td>3</td></tr>
<tr><td>0</td><td>3</td><td>6</td><td>9</td></tr>
<tr><td>1</td><td>4</td><td>7</td><td>10</td></tr>
<tr><td>2</td><td>5</td><td>8</td><td>11</td></tr>
</table>"</syntaxhighlight>
=={{header|Bracmat}}==
To make this interesting, the table is created as a Bracmat structure and then converted to XML by a library. This has the advantage that one doesn't have to worry about HTML entities and encoding issues. The contents of the cells are generated by a function that is instantiated with a starting number when the function makeTable is called. Notice the absence of loops.
<
= headTexts
minRowNr
Line 776 ⟶ 853:
Generator
Table
. !arg:(?headTexts.?minRowNr.?maxRowNr.?Generator)
& ( headCells
= cellText
Line 820 ⟶ 896:
)
: ?Table
& str$(
)
& makeTable
Line 832 ⟶ 908:
)
)
)</
{{out}}
<pre><table><thead align="right">
Line 844 ⟶ 920:
=={{header|C}}==
<
#include <stdlib.h>
Line 859 ⟶ 935:
return 0;
}</
{{out}} (wiki doesn't like tbody/thead tags):<table style="text-align:center; border: 1px solid"><th></th><th>X</th><th>Y</th><th>Z</th><tr><th>0</th><td>2777</td><td>886</td><td>9383</td></tr><tr><th>1</th><td>8335</td><td>7793</td><td>6915</td></tr><tr><th>2</th><td>6649</td><td>492</td><td>5386</td></tr><tr><th>3</th><td>27</td><td>2362</td><td>1421</td></tr></table>
=={{header|C sharp|C#}}==
<
using System.Text;
Line 896 ⟶ 972:
}
}
}</
=== More modern version ===
<
using System.Text;
using System.Xml;
Line 946 ⟶ 1,022:
}
}
</syntaxhighlight>
=={{header|C++}}==
<
#include <boost/array.hpp>
#include <string>
Line 1,016 ⟶ 1,092:
htmltable.close( ) ;
return 0 ;
}</
{{out}} ( of testtable.html ):
<
<head></head>
<body>
Line 1,035 ⟶ 1,111:
</body>
</html>
</syntaxhighlight>
=={{header|Clojure}}==
Using Hiccup (https://github.com/weavejester/hiccup):
<
(:use 'hiccup.core))
Line 1,049 ⟶ 1,125:
(<tr> :th ["" \X \Y \Z])
(for [n (range 1 4)]
(->> #(rand-int 10000) (repeatedly 3) (cons n) (<tr> :td)))])</
=={{header|CoffeeScript}}==
<
# This is one of many ways to create a table. CoffeeScript plays nice
# with any templating solution built for JavaScript, and of course you
Line 1,085 ⟶ 1,161:
html = table header_row, rows
console.log html
</syntaxhighlight>
{{out}}
Line 1,096 ⟶ 1,172:
<tr><th align='right'>5</th><td align='right'>4063</td><td align='right'>6726</td><td align='right'>1093</td></tr>
</table>
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
IDENTIFICATION DIVISION.
PROGRAM-ID. Table.
AUTHOR. Bill Gunshannon.
INSTALLATION. Home.
DATE-WRITTEN. 1 January 2022.
************************************************************
** Program Abstract:
** Data values are hardcoded in this example but they
** could come from anywhere. Computed, read from a
** file, input from the keyboard.
************************************************************
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT Table-File ASSIGN TO "index.html"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD Table-File
DATA RECORD IS Table-Record.
01 Table-Record.
05 Field1 PIC X(80).
WORKING-STORAGE SECTION.
01 Table-Data.
05 Line3.
10 Line3-Value1 PIC S9(4) VALUE 1234.
10 Line3-Value2 PIC S9(4) VALUE 23.
10 Line3-Value3 PIC S9(4) VALUE -123.
05 Line4.
10 Line4-Value1 PIC S9(4) VALUE 123.
10 Line4-Value2 PIC S9(4) VALUE 12.
10 Line4-Value3 PIC S9(4) VALUE -1234.
05 Line5.
10 Line5-Value1 PIC S9(4) VALUE 567.
10 Line5-Value2 PIC S9(4) VALUE 6789.
10 Line5-Value3 PIC S9(4) VALUE 3.
01 Table-HTML.
05 Line1 PIC X(16) VALUE
"<table border=1>".
05 Line2 PIC X(40) VALUE
"<th></th><th>X</th><th>Y</th><th>Z</th>".
05 Line3.
10 Line3-Field1 PIC X(31) VALUE
"<tr align=center><th>1</th><td>".
10 Line3-Value1 PIC -ZZZ9.
10 Line3-Field3 PIC X(9) VALUE
"</td><td>".
10 Line3-Value2 PIC -ZZZ9.
10 Line3-Field5 PIC X(9) VALUE
"</td><td>".
10 Line3-Value3 PIC -ZZZ9.
10 Line3-Field5 PIC X(10) VALUE
"</td></tr>".
05 Line4.
10 Line4-Field1 PIC X(31) VALUE
"<tr align=center><th>2</th><td>".
10 Line4-Value1 PIC -ZZZ9.
10 Line4-Field3 PIC X(9) VALUE
"</td><td>".
10 Line4-Value2 PIC -ZZZ9.
10 Line4-Field5 PIC X(9) VALUE
"</td><td>".
10 Line4-Value3 PIC -ZZZ9.
10 Line4-Field5 PIC X(10) VALUE
"</td></tr>".
05 Line5.
10 Line5-Field1 PIC X(31) VALUE
"<tr align=center><th>3</th><td>".
10 Line5-Value1 PIC -ZZZ9.
10 Line5-Field3 PIC X(9) VALUE
"</td><td>".
10 Line5-Value2 PIC -ZZZ9.
10 Line5-Field5 PIC X(9) VALUE
"</td><td>".
10 Line5-Value3 PIC -ZZZ9.
10 Line5-Field5 PIC X(10) VALUE
"</td></tr>".
05 Line6 PIC X(8) VALUE
"</table>".
PROCEDURE DIVISION.
Main-Program.
OPEN OUTPUT Table-File.
MOVE CORRESPONDING Table-Data TO Table-HTML.
PERFORM Write-Table.
CLOSE Table-File.
STOP RUN.
Write-Table.
WRITE Table-Record FROM Line1 OF Table-HTML
AFTER ADVANCING 1 LINE.
WRITE Table-Record FROM Line2 OF Table-HTML
AFTER ADVANCING 1 LINE.
WRITE Table-Record FROM Line3 OF Table-HTML
AFTER ADVANCING 1 LINE.
WRITE Table-Record FROM Line4 OF Table-HTML
AFTER ADVANCING 1 LINE.
WRITE Table-Record FROM Line5 OF Table-HTML
AFTER ADVANCING 1 LINE.
WRITE Table-Record FROM Line6 OF Table-HTML
AFTER ADVANCING 1 LINE.
END-PROGRAM.
</syntaxhighlight>
=={{header|Common Lisp}}==
Using Closure-HTML (https://common-lisp.net/project/closure/closure-html/) installed by the code itself via QuickLisp (http://quicklisp.org/).
<syntaxhighlight lang="common lisp">
(ql:quickload :closure-html)
(use-package :closure-html)
Line 1,113 ⟶ 1,308:
,@(loop repeat 3 collect `(td nil ,(format nil "~a" (random 10000)))))))
(make-string-sink))
</syntaxhighlight>
{{out}}
Line 1,120 ⟶ 1,315:
=={{header|D}}==
{{trans|C}}
<
import std.stdio, std.random;
Line 1,129 ⟶ 1,324:
i, uniform(0,1000), uniform(0,1000), uniform(0,1000));
writeln("</table>");
}</
{{out}}
<table style="text-align:center; border: 1px solid">
Line 1,140 ⟶ 1,335:
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 1,162 ⟶ 1,357:
Writeln('</table>');
Readln;
end.</
{{out}}
<
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td><td>7371</td><td>2659</td><td>1393</td></tr>
Line 1,171 ⟶ 1,366:
<tr><td>3</td><td>1316</td><td>1599</td><td>2086</td></tr>
<tr><td>4</td><td>4785</td><td>6612</td><td>5042</td></tr>
</table></
=={{header|EasyLang}}==
<syntaxhighlight>
print "<table border=1>"
write "<tr><th>"
for c$ in strchars "XYZ"
write "<th>" & c$
.
print ""
for r to 3
write "<tr align=right><td>" & r
for c to 3
write "<td>" & randint 200
.
print ""
.
print "</table>"
</syntaxhighlight>
{{out}}
<table border=1>
<tr><th><th>X<th>Y<th>Z
<tr align=right><td>1<td>18<td>151<td>118
<tr align=right><td>2<td>190<td>97<td>59
<tr align=right><td>3<td>61<td>179<td>127
</table>
=={{header|EchoLisp}}==
<
;; styles -
(style 'td "text-align:right")
Line 1,201 ⟶ 1,421:
(for ((i 1000)(row (rest table))) (emit-tag 'tr h-row (cons i row))))
</syntaxhighlight>
{{out}}
<
(define my-table '(("" X Y Z) (-1111 111 11) (22 -222 2222) (4422 0 42) (33 333 3333) (6666 666 66)))
Line 1,209 ⟶ 1,429:
(emit-tag 'table h-table my-table)
(string-join (stack->list html) " ")
</syntaxhighlight>
<table style='border-spacing: 10px;border:1px solid red'> <table style='border-spacing: 10px;border:1px solid red'> <tr style='undefined'> <th style='color:blue;'> </th> <th style='color:blue;'> X </th> <th style='color:blue;'> Y </th> <th style='color:blue;'> Z </th> </tr> <tr style='undefined'> <td style='text-align:right'> 0 </td> <td style='text-align:right'> -1111 </td> <td style='text-align:right'> 111 </td> <td style='text-align:right'> 11 </td> </tr> <tr style='undefined'> <td style='text-align:right'> 1 </td> <td style='text-align:right'> 22 </td> <td style='text-align:right'> -222 </td> <td style='text-align:right'> 2222 </td> </tr> <tr style='undefined'> <td style='text-align:right'> 2 </td> <td style='text-align:right'> 4422 </td> <td style='text-align:right'> 0 </td> <td style='text-align:right'> 42 </td> </tr> <tr style='undefined'> <td style='text-align:right'> 3 </td> <td style='text-align:right'> 33 </td> <td style='text-align:right'> 333 </td> <td style='text-align:right'> 3333 </td> </tr> <tr style='undefined'> <td style='text-align:right'> 4 </td> <td style='text-align:right'> 6666 </td> <td style='text-align:right'> 666 </td> <td style='text-align:right'> 66 </td> </tr> </table>
Line 1,215 ⟶ 1,435:
=={{header|Elixir}}==
<
defp put_rows(n) do
Enum.map_join(1..n, fn i ->
Line 1,233 ⟶ 1,453:
end
IO.puts Table.create_table</
{{out}}
<
<th></th><th>X</th><th>Y</th><th>Z</th>
<tr align=right><th>1</th><td>1362</td><td>1289</td><td>357</td></tr>
<tr align=right><th>2</th><td>1161</td><td>1345</td><td>1176</td></tr>
<tr align=right><th>3</th><td>391</td><td>62</td><td>609</td></tr>
</table></
{{out}}
Line 1,253 ⟶ 1,473:
=={{header|Erlang}}==
Both external_format/1 and html_table/3 are used by [[CSV_to_HTML_translation]]. Keep backwards compatibility when changing or change both.
<syntaxhighlight lang="erlang">
-module( create_html_table ).
Line 1,279 ⟶ 1,499:
remove_quoutes( String ) -> lists:flatten( string:tokens(String, "\"") ).
</syntaxhighlight>
{{out}}
<table border=1 cellpadding=10><tr><th> </th><th>X</th><th>Y</th><th>Z</th></tr><tr><td>1</td><td>6</td><td>563</td><td>476</td></tr><tr><td>2</td><td>401</td><td>310</td><td>59</td></tr><tr><td>3</td><td>579</td><td>990</td><td>331</td></tr></table>
=={{header|Euphoria}}==
<
puts(1," <tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>\n")
for i = 1 to 3 do
Line 1,293 ⟶ 1,513:
puts(1,"</tr>\n")
end for
puts(1,"</table>")</
{{out}}
<
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td><td>7978</td><td>7376</td><td>2382</td></tr>
<tr><td>2</td><td>3632</td><td>1947</td><td>8900</td></tr>
<tr><td>3</td><td>4098</td><td>1563</td><td>2762</td></tr>
</table></
=={{header|F_Sharp|F#}}==
<
type XmlDocument with
Line 1,347 ⟶ 1,567:
let xw = new XmlTextWriter(System.Console.Out)
xw.Formatting <- Formatting.Indented
xd.WriteContentTo(xw)</
{{out}}
(table part only)
Line 1,379 ⟶ 1,599:
=={{header|Factor}}==
<
: rnd ( -- n ) 10,000 random ;
Line 1,389 ⟶ 1,609:
${ 3 rnd rnd rnd }
}
[ simple-table. ] with-html-writer pprint-xml</
{{out}}
<
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Line 1,472 ⟶ 1,692:
</tr>
</table>
<br/></
=={{header|Forth}}==
Line 1,480 ⟶ 1,700:
Although this example is not a complete HTML interpreter, with these few extensions we can demonstrate mixing HTML tags with Forth code to generate the specified random numbers in-line with HTML. We could even use Forth to compile tags together and make HTML generating sub-routines like NETREXX does.
<
\ parser routines
Line 1,545 ⟶ 1,765:
</tr>
</table>
</syntaxhighlight>
{{Output}}
<pre>
Line 1,631 ⟶ 1,851:
===Source===
<syntaxhighlight lang="fortran">
MODULE PARAMETERS !Assorted oddities that assorted routines pick and choose from.
CHARACTER*5 I AM !Assuage finicky compilers.
Line 2,122 ⟶ 2,342:
CALL HTMLSTOP
END
</syntaxhighlight>
===Output===
As yet another demonstration of the vagaries of text interpretation, notice that the blank column heading, carefully converted to nbsp; (no leading ampersand here!) does ''not'' appear when the page is displayed, despite the "pre-formatted" hint.
Line 2,177 ⟶ 2,397:
</tBody>
</Table>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Dim As Integer ncols = 3, nrows = 4
Dim As Integer col, row
Print "<!DOCTYPE html>" & Chr(10) & "<html>"
Print "<head></head>" & Chr(10) & "<body>"
Print "<table border = 1 cellpadding = 10 cellspacing =0>"
For row = 0 To nrows
If row = 0 Then
Print "<tr><th></th>" ;
Else
Print "<tr><th>" & row & "</th>" ;
End If
For col = 1 To ncols
If row = 0 Then
Print "<th>" & Chr(87 + col) & "</th>" ;
Else
Print "<td align=""right"">" & Rnd(9999) & "</td>" ;
End If
Next col
Print "</tr>"
Next row
Print "</table>"
Print "</body>" & Chr(10) & "</html>"
Sleep</syntaxhighlight>
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
_W = 87
local fn BuildHTMLTable
NSinteger i, j
CFMutableStringRef mutStr = fn MutableStringWithCapacity(0)
MutableStringAppendString( mutStr, @"<table style=\"width:15%\">\n" )
MutableStringAppendString( mutStr, @" <tr><th></th>" )
for j = 1 to 3
MutableStringAppendFormat( mutStr, @"<th>%c</th>", _W + j )
next
MutableStringAppendString( mutStr, @"</tr>\n" )
for i = 1 to 3
MutableStringAppendFormat( mutStr, @" <tr><td>%d</td>", i )
for j = 1 to 3
MutableStringAppendFormat( mutStr, @"<td>%d</td>", rnd( 10000 ) )
next
MutableStringAppendString( mutStr, @"</tr>\n" )
next
MutableStringAppendString( mutStr, @"</table>" )
NSLog( @"%@", mutStr )
end fn
fn BuildHTMLTable
HandleEvents
</syntaxhighlight>
{{ HTML code output:}}
<pre>
<table style="width:15%">
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td><td>584</td><td>4574</td><td>2786</td></tr>
<tr><td>2</td><td>9573</td><td>4990</td><td>9345</td></tr>
<tr><td>3</td><td>1443</td><td>8258</td><td>2713</td></tr>
</table>
</pre>
{{ Actual output:}}
<table style="width:15%">
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td><td>584</td><td>4574</td><td>2786</td></tr>
<tr><td>2</td><td>9573</td><td>4990</td><td>9345</td></tr>
<tr><td>3</td><td>1443</td><td>8258</td><td>2713</td></tr>
</table>
=={{header|Go}}==
html/template is a package in the standard library.
<
import (
Line 2,215 ⟶ 2,516:
fmt.Println(err)
}
}</
{{out}}
<
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>0</td><td>0</td><td>1</td><td>2</td></tr>
Line 2,223 ⟶ 2,524:
<tr><td>2</td><td>6</td><td>7</td><td>8</td></tr>
<tr><td>3</td><td>9</td><td>10</td><td>11</td></tr>
</table></
=={{header|Groovy}}==
<
def createTable(columns, rowCount) {
Line 2,245 ⟶ 2,546:
}
println createTable(['X', 'Y', 'Z'], 3)</
{{out}}
<table style='border:1px solid;text-align:center;'>
Line 2,277 ⟶ 2,578:
{{libheader|blaze-html}}
<
import Control.Monad (forM_)
Line 2,303 ⟶ 2,604:
main = do
g <- getStdGen
putStrLn $ renderHtml $ makeTable ["", "X", "Y", "Z"] 3 g</
Or using the Shakespeare templates from the Yesod web framework.
{{libheader|shakespeare}}
<
import Data.List (elemIndex)
Line 2,358 ⟶ 2,659:
rowsOf _ [] = []
rowsOf n xs = take n xs : rowsOf n (drop n xs)
randomValues = randomRs (1, 9999) <$> getStdGen</
=={{header|Icon}} and {{header|Unicon}}==
<
printf("<table>\n <tr><th></th><th>X</th><th>Y</th><th>Z</th>")
every r := 1 to 4 do {
Line 2,370 ⟶ 2,671:
end
link printf </
{{libheader|Icon Programming Library}}
Line 2,376 ⟶ 2,677:
{{out}}
<
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td><td>3129</td><td>3294</td><td>7013</td></tr>
Line 2,382 ⟶ 2,683:
<tr><td>3</td><td>7001</td><td>963</td><td>4183</td></tr>
<tr><td>4</td><td>1695</td><td>1158</td><td>1240</td></tr>
</table></
=={{header|J}}==
Line 2,388 ⟶ 2,689:
We can define:
<
nm=. x-.LF
lf=. x-.nm
Line 2,397 ⟶ 2,698:
rows=. 'td' <@ele"1 ":&.>y
'table' ele ('tr',LF) <@ele ('th' ele x); rows
)</
With these definitions:
<
<table><tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>0</td><td>0</td><td>1</td><td>2</td></tr>
Line 2,408 ⟶ 2,709:
<tr><td>3</td><td>9</td><td>10</td><td>11</td></tr>
<tr><td>4</td><td>12</td><td>13</td><td>14</td></tr>
</table></
Or, if running under jhs:
<
to display the table inline, as html.
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.Random;
</syntaxhighlight>
<syntaxhighlight lang="java">
String generateHTMLTable() {
StringBuilder string = new StringBuilder();
string.append("<table border=\"1\">");
string.append(System.lineSeparator());
string.append("<tr>".indent(2));
string.append("<th width=\"40\"></th>".indent(4));
string.append("<th width=\"50\">X</th>".indent(4));
string.append("<th width=\"50\">Y</th>".indent(4));
string.append("<th width=\"50\">Z</th>".indent(4));
string.append("</tr>".indent(2));
Random random = new Random();
int number;
for (int countA = 0; countA < 10; countA++) {
string.append("<tr>".indent(2));
string.append("<td>%d</td>".formatted(countA).indent(4));
for (int countB = 0; countB < 3; countB++) {
number = random.nextInt(1, 9999);
string.append("<td>%,d</td>".formatted(number).indent(4));
}
string.append("</tr>".indent(2));
}
string.append("</table>");
return string.toString();
}
</syntaxhighlight>
<table border="1">
<tr>
<th width="40"></th>
<th width="50">X</th>
<th width="50">Y</th>
<th width="50">Z</th>
</tr>
<tr>
<td>0</td>
<td>7,382</td>
<td>8,078</td>
<td>1,200</td>
</tr>
<tr>
<td>1</td>
<td>121</td>
<td>3,051</td>
<td>1,795</td>
</tr>
<tr>
<td>2</td>
<td>9,310</td>
<td>6,264</td>
<td>7,526</td>
</tr>
<tr>
<td>3</td>
<td>2,294</td>
<td>367</td>
<td>3,753</td>
</tr>
<tr>
<td>4</td>
<td>9,680</td>
<td>3,921</td>
<td>6,582</td>
</tr>
<tr>
<td>5</td>
<td>5,308</td>
<td>5,232</td>
<td>3,035</td>
</tr>
<tr>
<td>6</td>
<td>1,252</td>
<td>1,468</td>
<td>7,081</td>
</tr>
<tr>
<td>7</td>
<td>1,808</td>
<td>1,143</td>
<td>4,439</td>
</tr>
<tr>
<td>8</td>
<td>7,133</td>
<td>1,747</td>
<td>8,636</td>
</tr>
<tr>
<td>9</td>
<td>4,882</td>
<td>9,367</td>
<td>5,056</td>
</tr>
</table>
<br />
An alternate demonstration
{{works with|Java|1.5+}}
This example assumes the header row is the first row in the given array and does not add row numbers. They will need to be added by the programmer when constructing the array.
<
public static String array2HTML(Object[][] array){
Line 2,443 ⟶ 2,843:
System.out.println(array2HTML(ints));
}
}</
{{out}}
<
=={{header|JavaScript}}==
===Iterative===
<
// normally, don't do this: at least name it something other than "a"
Line 2,483 ⟶ 2,883:
}
</script></head>
<body><script>make_table(5, 4)</script></body></html></
===Functional===
Line 2,490 ⟶ 2,890:
:#fold a more general HTML rendering function over a data tree.
<
'use strict';
Line 2,695 ⟶ 3,095:
// MAIN ---
return main();
})();</
{{Out}}
Line 2,760 ⟶ 3,160:
=={{header|jq}}==
{{works with|jq|1.4}}
<
"<tr>",
" \(.[] | "<td>\(.)</td>")",
Line 2,782 ⟶ 3,182:
length as $length
| . as $in
| [range(0;length) | [.+1] + $in[.]] | html_table(header);</
'''Example'''
<
[ [4,5,6],
[41, 51, 61],
Line 2,791 ⟶ 3,191:
# The first column has no header
data | html_table_with_sequence( ["", "X", "Y", "Z"] )</
{{out}}
<
<table>
<thead align = 'right'>
Line 2,823 ⟶ 3,223:
</tr>
</tbody
</table></
=={{header|Julia}}==
<
t, b = x
attrstr = join(" $n=\"$p\"" for (n, p) in attr)
Line 2,838 ⟶ 3,238:
body = "\n" * join(rows, '\n') * "\n"
table = tag(:table => string('\n', header, body, '\n'), :style => "width: 60%")
println(table)</
{{out}}
Line 2,855 ⟶ 3,255:
'''The raw html:'''
<
<th></th><th>X</th><th>Y</th><th>Z</th>
Line 2,865 ⟶ 3,265:
<tr><td style="font-weight: bold;">6</td><td>3896</td><td>9827</td><td>7006</td></tr>
</table></
=={{header|Kotlin}}==
<
import java.util.Random
Line 2,898 ⟶ 3,298:
}
println(sb.toString())
}</
{{out}}
Sample output:
<
<head>
<style>
Line 2,922 ⟶ 3,322:
</table>
</body>
</html></
=={{header|Lambdatalk}}==
Line 2,928 ⟶ 3,328:
Lambdatalk outputs standard HTML/CSS code sent to the web browser who does the job.
<
{table
{@ style="background:#ffe; width:50%;"}
Line 2,940 ⟶ 3,340:
{serie 1 3}}}}
{serie 1 3}}}
</syntaxhighlight>
{{out}}
Line 2,947 ⟶ 3,347:
=={{header|Lasso}}==
<
local(
Line 2,967 ⟶ 3,367:
#output -> append('</table>\n')
#output</
{{out}}
<table border=2 cellpadding=5 cellspace=0>
Line 3,001 ⟶ 3,401:
<br>
A time delay is needed to allow this, then the file is deleted.
<syntaxhighlight lang="lb">
nomainwin
Line 3,042 ⟶ 3,442:
end
end sub
</syntaxhighlight>
=={{header|Lingo}}==
<
str = "<table>"
Line 3,069 ⟶ 3,469:
put "</table>" after str
return str
end</
<
["X", "Y", "Z"],\
["1", "2", "3"],\
Line 3,082 ⟶ 3,482:
-- render the result in a text member (which only supports simple/ancient HTML)
m = new(#text)
m.text = "<html><body>"&htmlCode&"</body></html>"</
=={{header|Lua}}==
This function is written so as to take arbitrary table data. Its argument is a (Lua) table of (Lua) tables, where each sub-table is one row of the HTML table. The first row is assumed to be the column headings.
<
local html = "<table>\n<tr>\n<th></th>\n"
for _, heading in pairs(data[1]) do
Line 3,109 ⟶ 3,509:
}
print(htmlTable(tableData))</
{{out}}
<pre><table>
Line 3,139 ⟶ 3,539:
=={{header|M2000 Interpreter}}==
<
MODULE HtmlTable {
tag$=LAMBDA$ (a$)-> {
Line 3,176 ⟶ 3,576:
}
HtmlTable
</syntaxhighlight>
{{out}}
Line 3,213 ⟶ 3,613:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Print["<table>", "\n","<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>"]
Scan[Print["<tr><td>", #, "</td><td>", x, "</td><td>", x, "</td><td>","</td></tr>"] & , Range[3]]
Print["</table>"]</
{{out}}
Line 3,228 ⟶ 3,628:
=={{header|MATLAB}} / {{header|Octave}}==
<
fprintf(fid,'<table>\n <thead align = "right">\n');
if nargin<3,
Line 3,239 ⟶ 3,639:
fprintf(fid,' <tr><td>%2i</td><td>%5i</td><td>%5i</td><td>%5i</td></tr>\n', [1:size(table,1);table']);
fprintf(fid,' </tbody>\n</table>\n');
end</
{{out}}
Line 3,257 ⟶ 3,657:
=={{header|Modula-2}}==
<
FROM InOut IMPORT WriteCard, WriteLn, WriteString, WriteBf;
Line 3,303 ⟶ 3,703:
WriteLn;
WriteBf
END testCGI.</
=={{header|Modula-3}}==
{{trans|C}}
<syntaxhighlight lang="modula3">MODULE Table EXPORTS Main;
IMPORT IO, Random;
FROM Fmt IMPORT Int, F;
BEGIN
IO.Put("<table style=\"text-align:center; "
& "border: 1px solid\"><th></th>"
& "<th>X</th><th>Y</th><th><Z</th>");
WITH rand = NEW(Random.Default).init() DO
FOR I := 0 TO 3 DO
IO.Put(F("<tr><th>%s</th><td>%s</td>"
& "<td>%s</td><td>%s</td></tr>",
Int(I),
Int(rand.integer(0, 1000)),
Int(rand.integer(0, 1000)),
Int(rand.integer(0, 1000))));
END;
END;
END Table.</syntaxhighlight>
=={{header|Nanoquery}}==
{{trans|Ursa}}
This program outputs the HTML table to the console.
<
random = new(Random)
Line 3,326 ⟶ 3,749:
end for
println "</table>"</
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols nobinary
Line 3,451 ⟶ 3,874:
return html
</syntaxhighlight>
{{out}}
<div style="height:80ex;overflow:scroll;">
<
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
Line 3,522 ⟶ 3,945:
</body>
</html>
</syntaxhighlight>
</div>
{{out}}
Line 3,528 ⟶ 3,951:
=={{header|NewLISP}}==
<
; url: http://rosettacode.org/wiki/Create_an_HTML_table
; author: oofoe 2012-01-29
Line 3,583 ⟶ 4,006:
))
(exit)</
{{out}}
Line 3,643 ⟶ 4,066:
=={{header|Nim}}==
<
randomize()
template randTD():
proc randTR(x: int):
tr(td($x, style="font-weight: bold"), randTD, randTD, randTD)
Line 3,656 ⟶ 4,079:
randTR 3,
randTR 4,
randTR 5)</
{{out}}
<table><tr><th></th><th>X</th><th>Y</th><th>Z</th></tr><tr><td style="font-weight: bold">1</td><td>
Raw output:
<pre html5><table><tr><th></th><th>X</th><th>Y</th><th>Z</th></tr><tr><td style="font-weight: bold">1</td><td>
=={{header|Objeck}}==
<
class CreateTable {
function : Main(args : String[]) ~ Nil {
Line 3,698 ⟶ 4,121:
}
}
</syntaxhighlight>
=={{header|OCaml}}==
Line 3,704 ⟶ 4,127:
A simple <code>printf</code> method:
<
let buf = Buffer.create 1 in
let s = Buffer.add_string buf in
Line 3,726 ⟶ 4,149:
s "</tbody>";
s "</table>";
print_endline (Buffer.contents buf)</
=== With a dedicated library ===
Line 3,732 ⟶ 4,155:
Using the library [http://theorie.physik.uni-wuerzburg.de/~ohl/xhtml/ ocaml-xhtml]. With this library the validity of the pages is guaranteed by the OCaml type system.
<
let _td s = td [pcdata s]
Line 3,770 ⟶ 4,193:
let () =
pretty_print ~width:80 print_string my_page</
=== TyXml ===
Line 3,776 ⟶ 4,199:
The library [http://ocsigen.org/tyxml/ TyXml] contains a module for XHTML that provides the same interface than the previous <code>ocaml-xhtml</code> library.
<
#require "tyxml"
Line 3,800 ⟶ 4,223:
let my_html = X.html my_head my_body in
P.print print_endline my_html;
;;</
The previous function <code>make_table ()</code> produces a simple table, we can replace it by the function below to output a more complex table with <code>thead</code> and <code>tbody</code>:
<
let br = X.a_border 1 in
let th s = X.th [X.pcdata s] in
Line 3,814 ⟶ 4,237:
let my_tbody = X.tbody my_tr1 [my_tr2; my_tr3] in
let my_table = X.tablex ~thead:my_thead ~a:[br] my_tbody [] in
(my_table)</
=={{header|Oz}}==
As a complete web application, using the [https://github.com/wmeyer/roads/wiki "Roads"] web programming library. Connect your browser to http://localhost:8080/table after starting the program.
<
[Roads] = {Module.link ['x-ozlib://wmeyer/roads/Roads.ozf']}
Line 3,858 ⟶ 4,281:
{Roads.registerFunction table Table}
{Roads.run}</
=={{header|PARI/GP}}==
<
print("<table>\n<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>");
for(i=1,n,
Line 3,869 ⟶ 4,292:
);
print("</table>")
};</
{{out}}
<pre><table>
Line 3,879 ⟶ 4,302:
Note also the built-in <code>printtex</code> command, which allows the analogous task to be written as
<
=={{header|Pascal}}==
Line 3,887 ⟶ 4,310:
Opcodes of interest: SDC -- simple document; R!I -- ranged random integer
<
<@ DTBLIT>
<@ DTRLITLIT>
Line 3,903 ⟶ 4,326:
</@>
</@>
|Number Table</@></
=={{header|Perl}}==
<
my $rows = 5;
print '<table><thead><td>',
Line 3,918 ⟶ 4,341:
}
print "</tbody></table>";</
Note that this is a rather inane way (because of the inane task specification) of generating structured document. For serious work, one should use a module such as XML or HTML for well-formedness instead of this ad hoc method.
=={{header|Phix}}==
{{libheader|Phix/basics}}
===plain text approach===
<!--<syntaxhighlight lang="phix">-->
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"<table border=2>\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" <tr><th></th>"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"<th>%s</th>"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'W'</span><span style="color: #0000FF;">+</span><span style="color: #000000;">j</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"</tr>\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" <tr><td>%d</td>"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"<td>%d</td>"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"</tr>\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"</table>"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<table border=2>
Line 3,948 ⟶ 4,373:
'''The raw HTML'''
<pre>
===more structured===
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">include</span> <span style="color: #000000;">xml<span style="color: #0000FF;">.<span style="color: #000000;">e</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">contents</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{<span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">r<span style="color: #0000FF;">=<span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rowcontent</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{<span style="color: #0000FF;">}</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">thtd</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff<span style="color: #0000FF;">(<span style="color: #000000;">r<span style="color: #0000FF;">=<span style="color: #000000;">0<span style="color: #0000FF;">?<span style="color: #008000;">"th"<span style="color: #0000FF;">:<span style="color: #008000;">"td"<span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">c<span style="color: #0000FF;">=<span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">content</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff<span style="color: #0000FF;">(<span style="color: #000000;">r<span style="color: #0000FF;">=<span style="color: #000000;">0<span style="color: #0000FF;">?<span style="color: #008000;">"XYZ"<span style="color: #0000FF;">[<span style="color: #7060A8;">max<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">c<span style="color: #0000FF;">)<span style="color: #0000FF;">..<span style="color: #000000;">c<span style="color: #0000FF;">]<span style="color: #0000FF;">:</span>
<span style="color: #7060A8;">sprintf<span style="color: #0000FF;">(<span style="color: #008000;">"%d"<span style="color: #0000FF;">,<span style="color: #008080;">iff<span style="color: #0000FF;">(<span style="color: #000000;">c<span style="color: #0000FF;">=<span style="color: #000000;">0<span style="color: #0000FF;">?<span style="color: #000000;">r<span style="color: #0000FF;">:<span style="color: #7060A8;">rand<span style="color: #0000FF;">(<span style="color: #000000;">9999<span style="color: #0000FF;">)<span style="color: #0000FF;">)<span style="color: #0000FF;">)<span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">col</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xml_new_element<span style="color: #0000FF;">(<span style="color: #000000;">thtd<span style="color: #0000FF;">,<span style="color: #000000;">content<span style="color: #0000FF;">)</span>
<span style="color: #000000;">col</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xml_set_attribute<span style="color: #0000FF;">(<span style="color: #000000;">col<span style="color: #0000FF;">,<span style="color: #008000;">"style"<span style="color: #0000FF;">,<span style="color: #008000;">"text-align:right; padding: 5px;"<span style="color: #0000FF;">)</span>
<span style="color: #000000;">rowcontent</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append<span style="color: #0000FF;">(<span style="color: #000000;">rowcontent<span style="color: #0000FF;">,<span style="color: #000000;">col<span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">row</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xml_new_element<span style="color: #0000FF;">(<span style="color: #008000;">"tr"<span style="color: #0000FF;">,<span style="color: #000000;">rowcontent<span style="color: #0000FF;">)</span>
<span style="color: #000000;">contents</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append<span style="color: #0000FF;">(<span style="color: #000000;">contents<span style="color: #0000FF;">,<span style="color: #000000;">row<span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">table</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xml_new_element<span style="color: #0000FF;">(<span style="color: #008000;">"table"<span style="color: #0000FF;">,<span style="color: #000000;">contents<span style="color: #0000FF;">)</span>
<span style="color: #000000;">table</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xml_set_attribute<span style="color: #0000FF;">(<span style="color: #000000;">table<span style="color: #0000FF;">,</span> <span style="color: #008000;">"border"<span style="color: #0000FF;">,</span> <span style="color: #008000;">"2"<span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">doc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xml_new_doc<span style="color: #0000FF;">(<span style="color: #000000;">table<span style="color: #0000FF;">,<span style="color: #008000;">""<span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #7060A8;">xml_sprint<span style="color: #0000FF;">(<span style="color: #000000;">doc<span style="color: #0000FF;">)<span style="color: #0000FF;">)
<!--</syntaxhighlight>-->
{{out}}
<table border="2">
Line 4,005 ⟶ 4,433:
'''The raw XML'''
<pre>
<table border="2">
<tr>
<th style="text-align:right; padding: 5px;">Z</th>
<tr>
<td style="text-align:right; padding: 5px;">3339</td>
<tr>
<td style="text-align:right; padding: 5px;">8690</td>
<tr>
<td style="text-align:right; padding: 5px;">8167</td>
</tr>
</table></pre>
=={{header|PHP}}==
=== normal style ===
<
/**
* @author Elad Yosifon
Line 4,071 ⟶ 4,500:
echo $html;
</syntaxhighlight>
=== template engine style ===
<
/**
* @author Elad Yosifon
Line 4,109 ⟶ 4,538:
</body>
</html>
</syntaxhighlight>
=={{header|PicoLisp}}==
<
(<table> NIL NIL '(NIL (NIL "X") (NIL "Y") (NIL "Z"))
(for N 3
(<row> NIL N 124 456 789) ) )</
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
/* Create an HTML table. 6/2011 */
Line 4,163 ⟶ 4,592:
call create_table (headings, table_contents);
end create;</
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
# Converts Microsoft .NET Framework objects into HTML that can be displayed in a Web browser.
ConvertTo-Html -inputobject (Get-Date)
Line 4,177 ⟶ 4,606:
$object | ConvertTo-Html
</syntaxhighlight>
{{out}}
<syntaxhighlight lang="powershell">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
Line 4,205 ⟶ 4,634:
</table>
</body></html>
</syntaxhighlight>
When raw results are exported to Out-File cmdlet like this:
<syntaxhighlight lang="powershell">
$object | ConvertTo-Html | Out-File -FilePath $env:temp\test.html ; invoke-item $env:temp\test.html
</syntaxhighlight>
{{out}}
Line 4,219 ⟶ 4,648:
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
:- use_module(library(http/html_write)).
Line 4,235 ⟶ 4,664:
],
phrase(html(table([tr(\theader(Header)), \trows(Rows,1)])), Out, []),
print_html(Out).</
<
<tr><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td><td>7055</td><td>5334</td><td>5795</td></tr>
Line 4,243 ⟶ 4,672:
<tr><td>3</td><td>140</td><td>7607</td><td>8144</td></tr>
<tr><td>4</td><td>7090</td><td>475</td><td>4140</td></tr>
</table></
=={{header|PureBasic}}==
Line 4,250 ⟶ 4,679:
</font>
<syntaxhighlight lang="purebasic">
Title.s="Create an HTML table"
Line 4,291 ⟶ 4,720:
; RunProgram(FileName.s)
</syntaxhighlight>
{{out}}
Line 4,305 ⟶ 4,734:
'''The raw HTML'''
<
<html><head><title>Create an HTML table</title></head><body>
<table border=1 cellpadding=10 cellspacing=0>
Line 4,315 ⟶ 4,744:
</table>
</body></html>
</syntaxhighlight>
=={{header|Python}}==
<
import random
Line 4,330 ⟶ 4,759:
if __name__ == '__main__':
header = tag(tr=''.join(tag(th=txt) for txt in ',X,Y,Z'.split(','))) + '\n'
rows = '\n'.join(tag(tr=
+ ''.join(tag(td=rand9999())
for j in range(3
for i in range(1, 6))
table = tag(table='\n' + header + rows + '\n')
print(table)</
{{out}}
Line 4,348 ⟶ 4,777:
'''The raw HTML'''
<
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td style="font-weight: bold;">1</td><td>6040</td><td>4697</td><td>7055</td></tr>
Line 4,355 ⟶ 4,784:
<tr><td style="font-weight: bold;">4</td><td>5313</td><td>4396</td><td>1765</td></tr>
<tr><td style="font-weight: bold;">5</td><td>4013</td><td>5924</td><td>6082</td></tr>
</table></
Line 4,362 ⟶ 4,791:
{{Trans|JavaScript}}
{{works with|Python|3.6}}
<
import itertools
import random
Line 4,478 ⟶ 4,907:
if __name__ == '__main__':
main()</
{{Out}}
Line 4,511 ⟶ 4,940:
Raw HTML output
<
<caption>Table generated with Python</caption>
<tr>
Line 4,537 ⟶ 4,966:
<td>8882</td>
</tr>
</table></
=={{header|Racket}}==
<
#lang racket
Line 4,559 ⟶ 4,988:
(display-xml/content (xexpr->xml xexpr))
</syntaxhighlight>
=={{header|Raku}}==
Line 4,576 ⟶ 5,005:
This is certainly not the only or best way to generate HTML tables using Raku; just an example of one possible method.
<syntaxhighlight lang="raku"
my $rows = 5;
Line 4,588 ⟶ 5,017:
}
say tag('table', $table, 'cellspacing=4 style="text-align:right; border: 1px solid;"');</
{{out}}
Line 4,594 ⟶ 5,023:
=={{header|Rascal}}==
<
import util::Math;
Line 4,611 ⟶ 5,040:
html("Rosetta Code Table", table(rows)));
return "written";
}</
This will result in a simple html file. For example:
<
str: "written"</
{{out}}
<
{{out}}
Line 4,625 ⟶ 5,054:
=={{header|Red}}==
<
Create an HTML table
Line 4,673 ⟶ 5,102:
newline contents newline "</" tag ">"]]
</syntaxhighlight>
=={{header|Retro}}==
Using the '''casket::html'''' library which allows creation of HTML using quotes and combinators:
<
with casket::html'
Line 4,690 ⟶ 5,119:
[ [ "5" ] td [ rnd ] td [ rnd ] td [ rnd ] td ] tr
[ [ "6" ] td [ rnd ] td [ rnd ] td [ rnd ] td ] tr
] table</
=={{header|REXX}}==
Line 4,696 ⟶ 5,125:
<br>makes reading the file easier and also helps in debugging, but they could've been combined into a
<br>single '''lineout''' for succinctness.
<
parse arg rows . /*obtain optional argument from the CL.*/
if rows=='' | rows=="," then rows=5 /*no ROWS specified? Then use default.*/
Line 4,727 ⟶ 5,156:
/*──────────────────────────────────────────────────────────────────────────────────────*/
rnd: return right(random(0,maxRand*2)-maxRand,5) /*RANDOM doesn't generate negative ints*/
wrt: call lineout oFID,arg(1); say '══►' arg(1); w=w+1; return /*write.*/</
{{out|output|text= to the terminal (screen) using the default input of <tt> 5 </tt> rows:}}
<pre>
Line 4,830 ⟶ 5,259:
=={{header|Ring}}==
<
# Project: Create an HTML table
Line 4,865 ⟶ 5,294:
see str + nl
systemcmd("temp.htm")
</syntaxhighlight>
Output:
<pre>
Line 4,892 ⟶ 5,321:
Pure Ruby solution:
<
def r
rand(10000)
Line 4,915 ⟶ 5,344:
html << "</table>"
end
</syntaxhighlight>
Line 4,921 ⟶ 5,350:
{{libheader|REXML}}
<
table = [["", "X", "Y", "Z"],
[ 1, r, r, r],
Line 4,940 ⟶ 5,369:
formatter = REXML::Formatters::Pretty.new
formatter.compact = true
formatter.write(xtable, $stdout)</
{{out}}
<
<tr>
<td></td>
Line 4,968 ⟶ 5,397:
<td>9849</td>
</tr>
</table></
<table>
Line 4,998 ⟶ 5,427:
=={{header|Run BASIC}}==
<
for i = 1 to 5
html "<tr align=right>"
Line 5,006 ⟶ 5,435:
html "</tr>"
next i
html "</table>"</
{{out}}
Line 5,019 ⟶ 5,448:
=={{header|Rust}}==
{{libheader|rand|0.3}}
<
use rand::Rng;
Line 5,044 ⟶ 5,473:
println!("</table>");
}</
{{out}}
<
<tr><th>0</th><td>7101</td><td>9111</td><td>3446</td></tr>
<tr><th>1</th><td>426</td><td>9518</td><td>611</td></tr>
<tr><th>2</th><td>9693</td><td>419</td><td>4878</td></tr>
</table></
=={{header|Scala}}==
{{libheader|org.scala-lang.xml}}Scala has built-in support for XML, so you can freely mix XML literals into your Scala source code. This is nice, because instead of using strings to represent XML, you create XML literals that the compiler can understand and verify. This approach lets you easily generate dynamic XML by interweaving Scala code and XML in the same expressions.<
val data = List(List("X", "Y", "Z"), List(11, 12, 13), List(12, 22, 23), List(13, 32, 33))
Line 5,070 ⟶ 5,499:
println(generateTable(data))
}</
=={{header|Scheme}}==
{{works with|Guile}}{{works with|Gauche}}<
#("" "X" "Y" "Z")
#(1 1 2 3)
Line 5,093 ⟶ 5,522:
(display "</td>")))
(display "</tr>"))
(display "</table>")</
=={{header|Seed7}}==
<
const proc: main is func
Line 5,113 ⟶ 5,542:
end for;
writeln("</table>")
end func;</
{{out}}
Line 5,134 ⟶ 5,563:
=={{header|Sidef}}==
<
method _attr(Hash h) {
h.keys.sort.map {|k| %Q' #{k}="#{h{k}}"' }.join('')
Line 5,183 ⟶ 5,612:
)
}...
);</
{{out}}
Line 5,224 ⟶ 5,653:
=={{header|Snobol4}}==
<
output = "<table>"
output = " <tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>"
Line 5,237 ⟶ 5,666:
end
</syntaxhighlight>
{{out}}
<pre><table>
Line 5,269 ⟶ 5,698:
<
* val mkHtmlTable : ('a list * 'b list) -> ('a -> string * 'b -> string)
* -> (('a * 'b) -> string) -> string
Line 5,302 ⟶ 5,731:
(fn (a, b) => Real.toString (Math.pow (a, b))))
val _ = print (samplePage ())</
{{out}}
Line 5,325 ⟶ 5,754:
First, a program to write a Stata matrix to an HTML file.
<
local nr = rowsof(`1')
local nc = colsof(`1')
Line 5,361 ⟶ 5,790:
file write `f' "</html>" _n
file close `f'
end</
An example:
<
matrix rownames a = A B C
matrix colnames a = X Y Z
mat2html a magic.html</
=={{header|Tcl}}==
<
proc TAG {name args} {
set body [lindex $args end]
Line 5,411 ⟶ 5,840:
}]
}]
}]</
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
tablefile="table.html"
Line 5,435 ⟶ 5,864:
ENDACCESS d
BROWSE $tablefile
</syntaxhighlight>
{{out}}
<pre style='height:30ex;overflow:scroll'>
Line 5,455 ⟶ 5,884:
=={{header|UNIX Shell}}==
{{works with|ksh93}}
<
nameref d=$1
typeset -i idx=0
Line 5,492 ⟶ 5,921:
done
emit_table data</
{{out}}
<pre><table>
Line 5,511 ⟶ 5,940:
=={{header|Ursa}}==
This program outputs the HTML table to the console.
<
out "<table>" endl console
Line 5,528 ⟶ 5,957:
end for
out "</table>" endl console</
=={{header|VBA}}==
<syntaxhighlight lang="vba">
Public Sub BuildHTMLTable()
'simple HTML table, represented as a string matrix "cells"
Line 5,586 ⟶ 6,015:
HTMLWrap = sOpenTag & s & sClosingTag
End Function
</syntaxhighlight>
Subroutine BuildHTMLTable builds the HTML code as one big string.
Line 5,599 ⟶ 6,028:
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Set objFSO = CreateObject("Scripting.FileSystemObject")
Line 5,640 ⟶ 6,069:
objOutHTML.Close
Set objFSO = Nothing
</syntaxhighlight>
{{In}}
Line 5,672 ⟶ 6,101:
=={{header|Visual Basic .NET}}==
VB.NET has XML literals with embedded expressions (<%= ... %>) similar to [[#Scala|Scala]]. This example additionally uses LINQ query syntax.
<
Sub Main()
Const ROWS = 3
Line 5,700 ⟶ 6,129:
Console.WriteLine(result)
End Sub
End Module</
{{out}}
Line 5,765 ⟶ 6,194:
Since embedded expressions must contain expressions, anonymous iterator functions that are immediately called (notice the parentheses after End Function) are used to enable control constructs.
<
Sub Main()
Dim rand As Func(Of Integer, Integer) = AddressOf New Random(0).Next
Line 5,813 ⟶ 6,242:
Console.WriteLine(result)
End Sub
End Module</
{{out}}
Line 5,860 ⟶ 6,289:
A more idiomatic version that uses LINQ instead of loops:
<
Sub Main()
Dim rand As Func(Of Integer, Integer) = AddressOf New Random(0).Next
Line 5,895 ⟶ 6,324:
Console.WriteLine(result)
End Sub
End Module</
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "random" for Random
import "./fmt" for Fmt
var r = Random.new()
var sb = ""
var i = " " // indent
sb = sb + "<html>\n<head>\n"
sb = sb + "<style>\n"
sb = sb + "table, th, td { border: 1px solid black; }\n"
sb = sb + "th, td { text-align: right; }\n"
sb = sb + "</style>\n</head>\n<body>\n"
sb = sb + "<table style=\"width:60\%\">\n"
sb = sb + "%(i)<thead>\n"
sb = sb + "%(i)%(i)<tr><th></th>"
for (c in "XYZ") sb = sb + "<th>%(c)</th>"
sb = sb + "</tr>\n"
sb = sb + "%(i)</thead>\n"
sb = sb + "%(i)<tbody>\n"
var f = "%(i)%(i)<tr><td>$d</td><td>$d</td><td>$d</td><td>$d</td></tr>\n"
for (j in 1..4) sb = sb + Fmt.swrite(f, j, r.int(1e4), r.int(1e4), r.int(1e4))
sb = sb + "%(i)</tbody>\n"
sb = sb + "</table>\n"
sb = sb + "</body>\n</html>"
System.print(sb)</syntaxhighlight>
{{out}}
Sample output:
<pre>
<html>
<head>
<style>
table, th, td { border: 1px solid black; }
th, td { text-align: right; }
</style>
</head>
<body>
<table style="width:60%">
<thead>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
</thead>
<tbody>
<tr><td>1</td><td>7420</td><td>5394</td><td>7906</td></tr>
<tr><td>2</td><td>9319</td><td>4778</td><td>2984</td></tr>
<tr><td>3</td><td>401</td><td>3801</td><td>1613</td></tr>
<tr><td>4</td><td>1648</td><td>4234</td><td>6160</td></tr>
</tbody>
</table>
</body>
</html>
</pre>
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">include xpllib;
int I;
[Print("<table style=^"text-align:center; border: 1px solid^"><th></th>");
Print("<th>X</th><th>Y</th><th>Z</th>");
for I:= 0 to 4-1 do
Print("<tr><th>%d</th><td>%d</td><td>%d</td><td>%d</td></tr>", I,
Ran(10000), Ran(10000), Ran(10000));
Print("</table>");
]</syntaxhighlight>
{{out}}
<table style="text-align:center; border: 1px solid"><th></th><th>X</th><th>Y</th><th>Z</th><tr><th>0</th><td>5696</td><td>9421</td><td>1620</td></tr><tr><th>1</th><td>3509</td><td>9235</td><td>5287</td></tr><tr><th>2</th><td>2591</td><td>5458</td><td>7556</td></tr><tr><th>3</th><td>8857</td><td>9839</td><td>5446</td></tr></table>
=={{header|XSLT}}==
<
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="4.01" indent="yes"/>
Line 5,956 ⟶ 6,453:
</tr>
</xsl:template>
</xsl:stylesheet></
XSLT does not have a standard PRNG facility, so a list of numbers is given as input.
Line 5,962 ⟶ 6,459:
Sample input:
<
<numbers>
<number>1578</number>
Line 5,979 ⟶ 6,476:
<number>1403</number>
<number>4637</number>
</numbers></
{{out}}
<
<html>
<head>
Line 6,026 ⟶ 6,523:
</tr>
</table></body>
</html></
{{out}}
Line 6,069 ⟶ 6,566:
=={{header|zkl}}==
<
"<th></th><th>X</th><th>Y</th><th>Z</th><tr>";
table=Sink(table);
Line 6,077 ⟶ 6,574:
table.write("</tr>");
}
table.write("\n</table>\n").close().print();</
{{out}}
<table style="text-align:center; border: 1px solid"><th></th><th>X</th><th>Y</th><th>Z</th><tr>
|