Create an HTML table: Difference between revisions

m
m (→‎{{header|360 Assembly}}: Superfluous blanks suppressed)
 
(95 intermediate revisions by 39 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}}
<langsyntaxhighlight lang="360asm">* Create an HTML table 19/02/2017
CREHTML CSECT
USING CREHTML,R13
Line 72 ⟶ 103:
XDEC DS CL12
YREGS
END CREHTML</langsyntaxhighlight>
{{out}}
<pre>
Line 105 ⟶ 136:
</body></html>
</pre>
<html><head></head><body>
<table border=1 cellpadding=10 cellspacing=0>
<tr><th></th>
Line 133 ⟶ 163:
</tr>
</table>
 
</body></html>
=={{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 139 ⟶ 228:
We define a generic package to output HTML tables:
 
<langsyntaxhighlight Adalang="ada">with Ada.Strings.Unbounded;
 
generic
Line 157 ⟶ 246:
procedure Print(Items: Item_Array; Column_Heads: Header_Array);
 
end HTML_Table;</langsyntaxhighlight>
 
The implementation of the package:
 
<langsyntaxhighlight Adalang="ada">package body HTML_Table is
procedure Print(Items: Item_Array; Column_Heads: Header_Array) is
Line 216 ⟶ 305:
end Print;
 
end HTML_Table;</langsyntaxhighlight>
 
Here is the main program, using an instance of HTML_Table:
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Ada.Numerics.Discrete_Random, HTML_Table;
 
procedure Test_HTML_Table is
Line 251 ⟶ 340:
T.Print(Items => The_Table,
Column_Heads => (T.Convert("X"), T.Convert("Y"), T.Convert("Z")));
end Test_HTML_Table;</langsyntaxhighlight>
 
 
Each time you run the program, you get different random values for the table. {{out}}
<langsyntaxhighlight lang="html5"><table>
<thead align = "right">
<tr><th></th><td>X</td><td>Y</td><td>Z</td></tr>
Line 265 ⟶ 354:
<tr><td> 4</td><td> 31</td><td> 6897</td><td> 8265</td></tr>
</tbody>
</table></langsyntaxhighlight>
 
{{out}}
Line 278 ⟶ 367:
{{trans|ALGOL 68}}
Tested with Agena 2.9.5 Win32
<langsyntaxhighlight lang="agena">notNumbered := 0; # possible values for html table row numbering
numberedLeft := 1; # " " " " " " "
numberedRight := 2; # " " " " " " "
Line 332 ⟶ 421:
t.data := [ [ 1001, 1002, 1003 ], [ 21, 22, 23 ], [ 201, 202, 203 ] ];
writeHtmlTable( io.stdout, t )
epocs</langsyntaxhighlight>
{{out}}
<table cellspacing='0' colspacing='0' border='1'>
Line 362 ⟶ 451:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">INT not numbered = 0; # possible values for HTMLTABLE row numbering #
INT numbered left = 1; # " " " " " " #
INT numbered right = 2; # " " " " " " #
Line 439 ⟶ 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 )</langsyntaxhighlight>
{{out}}
<table cellspacing="0" colspacing="0" border="1">
Line 467 ⟶ 556:
</tr>
</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}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">out = <table style="text-align:center; border: 1px solid"><th></th><th>X</th><th>Y</th><th>Z</th><tr>
Loop 4
out .= "`r`n<tr><th>" A_Index "</th><td>" Rand() "</td><td>" Rand() "</td><td>" Rand() "</tr>"
Line 479 ⟶ 634:
Random, n, 1, % u
return n
}</langsyntaxhighlight>
{{out}}
<table style="text-align:center; border: 1px solid"><th></th><th>X</th><th>Y</th><th>Z</th><tr>
Line 490 ⟶ 645:
=={{header|AWK}}==
<langsyntaxhighlight AWKlang="awk">#!/usr/bin/awk -f
BEGIN {
print "<table>\n <thead align = \"right\">"
Line 498 ⟶ 653:
}
print " </tbody>\n</table>\n"
}</langsyntaxhighlight>
{{out}}
<pre><table>
Line 518 ⟶ 673:
</table>
</pre>
 
=={{header|Batch File}}==
This will overwrite any file by the name of "table.html" in the current directory.
<syntaxhighlight lang="dos">
@echo off
setlocal enabledelayedexpansion
 
:: It's easier and neater to create the variables holding the random 4 digit numbers ahead of time
for /l %%i in (1,1,12) do set /a rand%%i=!random! %% 9999
 
:: The command output of everything within the brackets is sent to the file "table.html", overwriting anything already in there
(
echo ^<html^>^<head^>^</head^>^<body^>
echo ^<table border=1 cellpadding=10 cellspacing=0^>
echo ^<tr^>^<th^>^</th^>
echo ^<th^>X^</th^>
echo ^<th^>Y^</th^>
echo ^<th^>Z^</th^>
echo ^</tr^>
echo ^<tr^>^<th^>1^</th^>
echo ^<td align="right"^>%rand1%^</td^>
echo ^<td align="right"^>%rand2%^</td^>
echo ^<td align="right"^>%rand3%^</td^>
echo ^</tr^>
echo ^<tr^>^<th^>2^</th^>
echo ^<td align="right"^>%rand4%^</td^>
echo ^<td align="right"^>%rand5%^</td^>
echo ^<td align="right"^>%rand6%^</td^>
echo ^</tr^>
echo ^<tr^>^<th^>3^</th^>
echo ^<td align="right"^>%rand7%^</td^>
echo ^<td align="right"^>%rand8%^</td^>
echo ^<td align="right"^>%rand9%^</td^>
echo ^</tr^>
echo ^<tr^>^<th^>4^</th^>
echo ^<td align="right"^>%rand10%^</td^>
echo ^<td align="right"^>%rand11%^</td^>
echo ^<td align="right"^>%rand12%^</td^>
echo ^</tr^>
echo ^</table^>
echo ^</body^>^</html^>
) > table.html
start table.html
</syntaxhighlight>
{{out}}
<pre>
<html><head></head><body>
<table border=1 cellpadding=10 cellspacing=0>
<tr><th></th>
<th>X</th>
<th>Y</th>
<th>Z</th>
</tr>
<tr><th>1</th>
<td align="right">6608</td>
<td align="right">3993</td>
<td align="right">4797</td>
</tr>
<tr><th>2</th>
<td align="right">498</td>
<td align="right">4475</td>
<td align="right">2218</td>
</tr>
<tr><th>3</th>
<td align="right">3742</td>
<td align="right">9842</td>
<td align="right">1729</td>
</tr>
<tr><th>4</th>
<td align="right">9251</td>
<td align="right">478</td>
<td align="right">3945</td>
</tr>
</table>
</body></html>
</pre>
 
<table border=1 cellpadding=10 cellspacing=0>
<tr><th></th>
<th>X</th>
<th>Y</th>
<th>Z</th>
</tr>
<tr><th>1</th>
<td align="right">6608</td>
<td align="right">3993</td>
<td align="right">4797</td>
</tr>
<tr><th>2</th>
<td align="right">498</td>
<td align="right">4475</td>
<td align="right">2218</td>
</tr>
<tr><th>3</th>
<td align="right">3742</td>
<td align="right">9842</td>
<td align="right">1729</td>
</tr>
<tr><th>4</th>
<td align="right">9251</td>
<td align="right">478</td>
<td align="right">3945</td>
</tr>
</table>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
Uses BBC BASIC's *spool command to create a file.
<langsyntaxhighlight lang="bbcbasic"> ncols% = 3
nrows% = 4
Line 552 ⟶ 811:
SYS "ShellExecute", @hwnd%, 0, "temp.htm", 0, 0, 1
</syntaxhighlight>
</lang>
{{out}}
<pre>&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;
Line 570 ⟶ 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.
<langsyntaxhighlight lang="bracmat">( ( makeTable
= headTexts
minRowNr
Line 581 ⟶ 853:
Generator
Table
. !arg:(?headTexts.?minRowNr.?maxRowNr.?Generator)
. get$"xmlio.bra" { A library that converts from Bracmat format to XML or HTML }
& !arg:(?headTexts.?minRowNr.?maxRowNr.?Generator)
& ( headCells
= cellText
Line 625 ⟶ 896:
)
: ?Table
& str$((XMLIO.convert)toML$!Table) { Call library function to create { Create HTML }
)
& makeTable
Line 637 ⟶ 908:
)
)
)</langsyntaxhighlight>
{{out}}
<pre>&lt;table&gt;&lt;thead align="right"&gt;
Line 649 ⟶ 920:
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 664 ⟶ 935:
 
return 0;
}</langsyntaxhighlight>
{{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#}}==
<syntaxhighlight lang="csharp">using System;
using System.Text;
 
namespace prog
{
class MainClass
{
public static void Main (string[] args)
{
StringBuilder s = new StringBuilder();
Random rnd = new Random();
s.AppendLine("<table>");
s.AppendLine("<thead align = \"right\">");
s.Append("<tr><th></th>");
for(int i=0; i<3; i++)
s.Append("<td>" + "XYZ"[i] + "</td>");
s.AppendLine("</tr>");
s.AppendLine("</thead>");
s.AppendLine("<tbody align = \"right\">");
for( int i=0; i<3; i++ )
{
s.Append("<tr><td>"+i+"</td>");
for( int j=0; j<3; j++ )
s.Append("<td>"+rnd.Next(10000)+"</td>");
s.AppendLine("</tr>");
}
s.AppendLine("</tbody>");
s.AppendLine("</table>");
Console.WriteLine( s );
}
}
}</syntaxhighlight>
 
=== More modern version ===
<syntaxhighlight lang="csharp">using System;
using System.Text;
using System.Xml;
 
namespace N
{
public class T
{
public static void Main()
{
var headers = new [] { "", "X", "Y", "Z" };
var cols = headers.Select(name =>
new XElement(
"th",
name,
new XAttribute("text-align", "center")
)
);
var rows = Enumerable.Range(0, 4).Select(ri =>
new XElement(
"tr",
new XElement("td", ri),
Enumerable.Range(0, 4).Select(ci =>
new XElement(
"td",
ci,
new XAttribute("text-align", "center")
)
)
)
);
var xml = new XElement(
"table",
new XElement(
"thead",
new XElement("tr", cols),
new XElement("tbody", rows)
)
);
Console.WriteLine(xml.ToString());
}
}
}
</syntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <fstream>
#include <boost/array.hpp>
#include <string>
Line 735 ⟶ 1,092:
htmltable.close( ) ;
return 0 ;
}</langsyntaxhighlight>
{{out}} ( of testtable.html ):
<LANGsyntaxhighlight html5lang="html"5><html>
<head></head>
<body>
Line 754 ⟶ 1,111:
</body>
</html>
</syntaxhighlight>
</LANG>
=={{header|C sharp}}==
<lang csharp>using System;
using System.Text;
 
namespace prog
{
class MainClass
{
public static void Main (string[] args)
{
StringBuilder s = new StringBuilder();
Random rnd = new Random();
s.AppendLine("<table>");
s.AppendLine("<thead align = \"right\">");
s.Append("<tr><th></th>");
for(int i=0; i<3; i++)
s.Append("<td>" + "XYZ"[i] + "</td>");
s.AppendLine("</tr>");
s.AppendLine("</thead>");
s.AppendLine("<tbody align = \"right\">");
for( int i=0; i<3; i++ )
{
s.Append("<tr><td>"+i+"</td>");
for( int j=0; j<3; j++ )
s.Append("<td>"+rnd.Next(10000)+"</td>");
s.AppendLine("</tr>");
}
s.AppendLine("</tbody>");
s.AppendLine("</table>");
Console.WriteLine( s );
}
}
}</lang>
 
=== More modern version ===
<lang csharp>using System;
using System.Text;
using System.Xml;
 
namespace N
{
public class T
{
public static void Main()
{
var headers = new [] { "", "X", "Y", "Z" };
var cols = headers.Select(name =>
new XElement(
"th",
name,
new XAttribute("text-align", "center")
)
);
var rows = Enumerable.Range(0, 4).Select(ri =>
new XElement(
"tr",
new XElement("td", ri),
Enumerable.Range(0, 4).Select(ci =>
new XElement(
"td",
ci,
new XAttribute("text-align", "center")
)
)
)
);
var xml = new XElement(
"table",
new XElement(
"thead",
new XElement("tr", cols),
new XElement("tbody", rows)
)
);
Console.WriteLine(xml.ToString());
}
}
}
</lang>
=={{header|Clojure}}==
Using Hiccup (https://github.com/weavejester/hiccup):
<langsyntaxhighlight lang="clojure">(ns rosettacode.html-table
(:use 'hiccup.core))
 
Line 852 ⟶ 1,125:
(<tr> :th ["" \X \Y \Z])
(for [n (range 1 4)]
(->> #(rand-int 10000) (repeatedly 3) (cons n) (<tr> :td)))])</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="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 887 ⟶ 1,161:
html = table header_row, rows
console.log html
</syntaxhighlight>
</lang>
 
{{out}}
Line 898 ⟶ 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">
<lang Common Lisp>
(ql:quickload :closure-html)
(use-package :closure-html)
Line 915 ⟶ 1,308:
,@(loop repeat 3 collect `(td nil ,(format nil "~a" (random 10000)))))))
(make-string-sink))
</syntaxhighlight>
</lang>
 
{{out}}
Line 922 ⟶ 1,315:
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.random;
 
Line 931 ⟶ 1,324:
i, uniform(0,1000), uniform(0,1000), uniform(0,1000));
writeln("</table>");
}</langsyntaxhighlight>
{{out}}
<table style="text-align:center; border: 1px solid">
Line 942 ⟶ 1,335:
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="delphi">program CreateHTMLTable;
 
{$APPTYPE CONSOLE}
Line 964 ⟶ 1,357:
Writeln('</table>');
Readln;
end.</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="html5"><table>
<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 973 ⟶ 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></langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="scheme">
;; styles -
(style 'td "text-align:right")
Line 1,003 ⟶ 1,421:
(for ((i 1000)(row (rest table))) (emit-tag 'tr h-row (cons i row))))
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
(define my-table '(("" X Y Z) (-1111 111 11) (22 -222 2222) (4422 0 42) (33 333 3333) (6666 666 66)))
 
Line 1,011 ⟶ 1,429:
(emit-tag 'table h-table my-table)
(string-join (stack->list html) " ")
</syntaxhighlight>
</lang>
 
<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,017 ⟶ 1,435:
=={{header|Elixir}}==
 
<langsyntaxhighlight Elixirlang="elixir">defmodule Table do
defp put_rows(n) do
Enum.map_join(1..n, fn i ->
Line 1,035 ⟶ 1,453:
end
 
IO.puts Table.create_table</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="html5"><table border=1>
<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></langsyntaxhighlight>
 
{{out}}
Line 1,055 ⟶ 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">
<lang Erlang>
-module( create_html_table ).
 
Line 1,081 ⟶ 1,499:
 
remove_quoutes( String ) -> lists:flatten( string:tokens(String, "\"") ).
</syntaxhighlight>
</lang>
{{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}}==
<langsyntaxhighlight lang="euphoria">puts(1,"<table>\n")
puts(1," <tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>\n")
for i = 1 to 3 do
Line 1,095 ⟶ 1,513:
puts(1,"</tr>\n")
end for
puts(1,"</table>")</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="html5"><table>
<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></langsyntaxhighlight>
 
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System.Xml
 
type XmlDocument with
Line 1,150 ⟶ 1,567:
let xw = new XmlTextWriter(System.Console.Out)
xw.Formatting <- Formatting.Indented
xd.WriteContentTo(xw)</langsyntaxhighlight>
{{out}}
(table part only)
Line 1,180 ⟶ 1,597:
</tr>
</table>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: html.streams literals prettyprint random xml.writer ;
 
: rnd ( -- n ) 10,000 random ;
 
{
{ "" "X" "Y" "Z" }
${ 1 rnd rnd rnd }
${ 2 rnd rnd rnd }
${ 3 rnd rnd rnd }
}
[ simple-table. ] with-html-writer pprint-xml</syntaxhighlight>
{{out}}
<syntaxhighlight lang="html5"><table style="display: inline-table; border-collapse: collapse;">
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
X
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Y
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Z
</td>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<div style="display: inline-block; ">
1
</div>
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<div style="display: inline-block; ">
9463
</div>
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<div style="display: inline-block; ">
3201
</div>
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<div style="display: inline-block; ">
9044
</div>
</td>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<div style="display: inline-block; ">
2
</div>
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<div style="display: inline-block; ">
3152
</div>
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<div style="display: inline-block; ">
6685
</div>
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<div style="display: inline-block; ">
928
</div>
</td>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<div style="display: inline-block; ">
3
</div>
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<div style="display: inline-block; ">
696
</div>
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<div style="display: inline-block; ">
3803
</div>
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<div style="display: inline-block; ">
5079
</div>
</td>
</tr>
</table>
<br/></syntaxhighlight>
 
=={{header|Forth}}==
Printing out a group of HTML text strings is not difficult for Forth or most computer languages.
Line 1,186 ⟶ 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.
 
<langsyntaxhighlight Forthlang="forth">include random.hsf
 
\ parser routines
Line 1,251 ⟶ 1,765:
</tr>
</table>
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 1,337 ⟶ 1,851:
 
===Source===
<syntaxhighlight lang="fortran">
<lang Fortran>
MODULE PARAMETERS !Assorted oddities that assorted routines pick and choose from.
CHARACTER*5 I AM !Assuage finicky compilers.
Line 1,828 ⟶ 2,342:
CALL HTMLSTOP
END
</syntaxhighlight>
</lang>
===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 1,883 ⟶ 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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,921 ⟶ 2,516:
fmt.Println(err)
}
}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="html5"><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 1,929 ⟶ 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></langsyntaxhighlight>
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">import groovy.xml.MarkupBuilder
 
def createTable(columns, rowCount) {
Line 1,951 ⟶ 2,546:
}
 
println createTable(['X', 'Y', 'Z'], 3)</langsyntaxhighlight>
{{out}}
<table style='border:1px solid;text-align:center;'>
Line 1,983 ⟶ 2,578:
{{libheader|blaze-html}}
 
<syntaxhighlight lang="haskell">import Data.List (unfoldr)
<lang haskell>#!/usr/bin/runhaskell
 
import Control.Monad (forM_)
import System.Random
import Data.List as L
 
import qualified Text.Blaze.Html5 as B
import Text.Blaze.Html.Renderer.Pretty (renderHtml)
 
import System.Random (RandomGen, getStdGen, randomRs, split)
makeTable :: RandomGen g => [String] -> Int -> g -> Html
 
makeTable
:: RandomGen g
=> [String] -> Int -> g -> B.Html
makeTable headings nRows gen =
B.table $ do
do B.thead $ B.tr $ forM_ (LB.map toHtml <$> headings) B.th
B.tbody $
tbody $ forM_ (zip [1 .. nRows] $ unfoldr (Just . split) gen)
(\(x,g) -> tr $ forM_ (take (length headings)
(zip [1 .. nRows] $ unfoldr (x:randomRsJust (1000,9999). g)split) (td . toHtml)gen)
(\(x, g) ->
B.tr $
forM_
(take (length headings) (x : randomRs (1000, 9999) g))
(B.td . B.toHtml))
 
main :: IO ()
main = do
g <- getStdGen
putStrLn $ renderHtml $ makeTable ["", "X", "Y", "Z"] 3 g</langsyntaxhighlight>
 
Or using the Shakespeare templates from the Yesod web framework.
{{libheader|shakespeare}}
<syntaxhighlight lang="haskell">{-# LANGUAGE QuasiQuotes #-}
 
import Data.List (elemIndex)
import Text.Hamlet (shamlet, Html)
import Text.Cassius (Css, renderCss, cassius)
import Text.Blaze.Html.Renderer.String (renderHtml)
import System.Random (getStdGen, randomRs)
 
styles :: p -> Css
styles = [cassius|
table, th, td
border: 1px solid black
border-collapse: collapse
th, td
padding: 15px
th, .rowLabel
background-color: #895
td
text-align: right
|]
 
renderTable :: [[Int]] -> Html
renderTable xs = [shamlet|
$doctype 5
<html>
<head>
<style>
#{renderCss $ styles undefined}
<body>
<table>
<tr>
<th>
$forall header <- headers
<th>#{header}
$forall row <- xs
<tr>
$maybe index <- elemIndex row xs
<td .rowLabel>#{index + 1}
$nothing
<td>?
$forall cell <- row
<td>#{cell}
|]
where
headers = ['X', 'Y', 'Z']
 
main :: IO ()
main = renderHtml . renderTable . rowsOf 3 . take 9 <$> randomValues >>= putStrLn
where
rowsOf _ [] = []
rowsOf n xs = take n xs : rowsOf n (drop n xs)
randomValues = randomRs (1, 9999) <$> getStdGen</syntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main()
printf("<table>\n <tr><th></th><th>X</th><th>Y</th><th>Z</th>")
every r := 1 to 4 do {
Line 2,014 ⟶ 2,671:
end
 
link printf </langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 2,020 ⟶ 2,677:
 
{{out}}
<langsyntaxhighlight lang="html5"><table>
<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,026 ⟶ 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></langsyntaxhighlight>
 
=={{header|J}}==
Line 2,032 ⟶ 2,689:
We can define:
 
<langsyntaxhighlight lang="j">ele=:4 :0
nm=. x-.LF
lf=. x-.nm
Line 2,041 ⟶ 2,698:
rows=. 'td' <@ele"1 ":&.>y
'table' ele ('tr',LF) <@ele ('th' ele x); rows
)</langsyntaxhighlight>
 
With these definitions:
 
<langsyntaxhighlight lang="j"> ('';;:'X Y Z') hTbl ":&.>(i.5),.i.5 3
<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,052 ⟶ 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></langsyntaxhighlight>
 
Or, if running under jhs:
 
<langsyntaxhighlight lang="j">jhtml ('';;:'X Y Z') hTbl ":&.>(i.5),.i.5 3</langsyntaxhighlight>
 
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.
<langsyntaxhighlight lang="java5">public class HTML {
 
public static String array2HTML(Object[][] array){
Line 2,087 ⟶ 2,843:
System.out.println(array2HTML(ints));
}
}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="html5"><table><th></th><th>X</th><th>Y</th><th>Z</th><tr><td>1</td><td>1</td><td>2</td><td>3</td></tr><tr><td>2</td><td>4</td><td>5</td><td>6</td></tr><tr><td>3</td><td>7</td><td>8</td><td>9</td></tr><tr><td>4</td><td>10</td><td>11</td><td>12</td></tr></table></langsyntaxhighlight>
 
=={{header|JavaScript}}==
===Iterative===
<lang JavaScript><html><head><title>Table maker</title><script type="application/javascript">
<syntaxhighlight lang="javascript"><html><head><title>Table maker</title><script type="application/javascript">
 
// normally, don't do this: at least name it something other than "a"
Line 2,125 ⟶ 2,883:
}
</script></head>
<body><script>make_table(5, 4)</script></body></html></langsyntaxhighlight>
 
===Functional===
Alternatively we could:
:#Separate the data definition from the HTML generation, and
:#fold a more general HTML rendering function over a data tree.
 
<syntaxhighlight lang="javascript">(() => {
Or, as an alternative to iterative assembly of the HTML string, we could:
'use strict';
:#Separate the data definition from the HTML rendering, and
:#generate the HTML from a functional expression which, for ease of legibility and refactoring, visually resembles the structure of the HTML table code itself.
 
// HTML ---------------------------------------------
<lang JavaScript>(function (lngCols, lngRows) {
 
// treeHTML :: tree
//range(5, 20) --> [5..20]
// {tag :: String, text :: String, kvs :: Dict}
//range('a', 'n') --> ['a'..'n']
// -> String
function range(m, n) {
varconst blnAlphatreeHTML = typeof mtree === 'string',>
foldTree(
iFirst = blnAlpha ? m.charCodeAt(0) : m,
(x, xs) => `<${x.tag + attribString(x.kvs)}>` + (
lstInt = Array.apply(
null, 'text' in x ? (
Array((blnAlpha ? n.charCodeAt(0) : n) - iFirst + 1) x.text
).map(function (x, i ) {: '\n'
return iFirst ) + concat(xs) + i;`</${x.tag}>\n`)(
}); tree
);
 
// attribString :: Dict -> String
return blnAlpha ? lstInt.map(
const attribString function= (x)dct {=>
returndct ? String.fromCharCode(x);
' ' + Object.keys(dct)
}
) : lstInt; .reduce(
(a, k) => a + k + '="' + dct[k] + '" ', ''
}
).trim()
) : '';
// Letter label for first column (last column will be 'Z')
var strFirstCol = String.fromCharCode('Z'.charCodeAt(0) - (lngCols - 1));
var lstData = [[''].concat(range(strFirstCol, 'Z'))].concat(
range(1, lngRows).map(
function (row) {
return [row].concat(
range(1, lngCols).map(
function () {
return Math.floor(
Math.random() * 9999
);
}
)
);
}
)
);
 
// TEST ---------------------------------------------
return [
'<tableconst main = () =>', {
const
tableStyle = {
style: "width:25%; border:2px solid silver;"
},
trStyle = {
style: "border:1px solid silver;text-align:right;"
},
strCaption = 'Table generated by JS';
 
const
' <thead style = "text-align: right;">',
' ' + lstData[0].reduce( n = 3,
function (a, s)colNames {= take(n)(enumFrom('A')),
returndataRows a= + '<th>' + s + '</th>';map(
}, '<tr x =>' Tuple(x)(map(randomRInt(100)(9999))(
) + '</tr>', colNames
)))(take(n)(enumFrom(1)));
' </thead>',
 
const
' <tbody style = "text-align: right;">',
// TABLE AS TREE STRUCTURE -----------------
lstData.slice(1).map(
function (row) tableTree = Node({
return ' ' + row.reduce( tag: 'table',
function (a, s) { kvs: tableStyle
return a + '<td>' + s + '</td>';},
}, '<tr>' append([
) + '</tr>'; Node({
tag: 'caption',
text: 'Table source generated by JS'
}),
// HEADER ROW -----------------------
Node({
tag: 'tr',
},
map(k => Node({
tag: 'th',
kvs: {
style: "text-align:right;"
},
text: k
}))(cons('')(colNames))
)
// DATA ROWS ------------------------
])(map(tpl => Node({
tag: 'tr',
kvs: trStyle
}, cons(
Node({
tag: 'th',
text: fst(tpl)
}))(
map(v => Node({
tag: 'td',
text: v.toString()
}))(snd(tpl))
)))(dataRows))
);
 
// Return a value and/or apply console.log to it.
// (JS embeddings vary in their IO channels)
const strHTML = treeHTML(tableTree);
return (
console.log(strHTML)
//strHTML
);
};
 
 
// GENERIC FUNCTIONS --------------------------------
 
// Node :: a -> [Tree a] -> Tree a
const Node = (v, xs) => ({
type: 'Node',
root: v,
nest: xs || []
});
 
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a => b => ({
type: 'Tuple',
'0': a,
'1': b,
length: 2
});
 
// append (++) :: [a] -> [a] -> [a]
// append (++) :: String -> String -> String
const append = xs => ys => xs.concat(ys);
 
// chr :: Int -> Char
const chr = String.fromCodePoint;
 
// concat :: [[a]] -> [a]
// concat :: [String] -> String
const concat = xs =>
0 < xs.length ? (() => {
const unit = 'string' !== typeof xs[0] ? (
[]
) : '';
return unit.concat.apply(unit, xs);
})() : [];
 
// cons :: a -> [a] -> [a]
const cons = x => xs => [x].concat(xs);
 
// enumFrom :: a -> [a]
function* enumFrom(x) {
let v = x;
while (true) {
yield v;
v = succ(v);
}
).join('\n'),}
' </tbody>',
 
// enumFromToChar :: Char -> Char -> [Char]
'</table>'
const enumFromToChar = m => n => {
].join('\n');
const [intM, intN] = [m, n].map(
x => x.charCodeAt(0)
);
return Array.from({
length: Math.floor(intN - intM) + 1
}, (_, i) => String.fromCodePoint(intM + i));
};
 
})(3, 4); // (3foldTree columns:: (a --> [X..Zb] -> b), (4-> rowsTree a --> [1..4])b
const foldTree = f => tree => {
</lang>
const go = node =>
f(node.root, node.nest.map(go));
return go(tree);
};
 
// fst :: (a, b) -> a
{{out}}
const fst = tpl => tpl[0];
<pre><table>
 
<thead style = "text-align: right;">
// isChar :: a -> Bool
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
const isChar = x =>
</thead>
('string' === typeof x) && (1 === x.length);
<tbody style = "text-align: right;">
 
<tr><td>1</td><td>9930</td><td>1530</td><td>3070</td></tr>
// map :: (a -> b) -> [a] -> [b]
<tr><td>2</td><td>5406</td><td>9285</td><td>311</td></tr>
const map = f => xs =>
<tr><td>3</td><td>647</td><td>6491</td><td>3684</td></tr>
(Array.isArray(xs) ? (
<tr><td>4</td><td>2274</td><td>4709</td><td>1140</td></tr>
xs
</tbody>
) : xs.split('')).map(f);
</table></pre>
 
// ord :: Char -> Int
const ord = c => c.codePointAt(0);
 
// randomRInt :: Int -> Int -> () -> Int
const randomRInt = low => high => () =>
low + Math.floor(
(Math.random() * ((high - low) + 1))
);
 
// snd :: (a, b) -> b
const snd = tpl => tpl[1];
 
// succ :: Enum a => a -> a
const succ = x =>
isChar(x) ? (
chr(1 + ord(x))
) : isNaN(x) ? (
undefined
) : 1 + x;
 
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
const take = n => xs =>
'GeneratorFunction' !== xs.constructor.constructor.name ? (
xs.slice(0, n)
) : [].concat.apply([], Array.from({
length: n
}, () => {
const x = xs.next();
return x.done ? [] : [x.value];
}));
 
// MAIN ---
return main();
})();</syntaxhighlight>
{{Out}}
 
<table style="width:25%; border:2px solid silver;">
<caption>Table source generated by JS</caption>
<tr>
<th style="text-align:right;"></th>
<th style="text-align:right;">A</th>
<th style="text-align:right;">B</th>
<th style="text-align:right;">C</th>
</tr>
<tr style="border:1px solid silver;text-align:right;">
<th>1</th>
<td>8464</td>
<td>1650</td>
<td>8275</td>
</tr>
<tr style="border:1px solid silver;text-align:right;">
<th>2</th>
<td>547</td>
<td>9794</td>
<td>8690</td>
</tr>
<tr style="border:1px solid silver;text-align:right;">
<th>3</th>
<td>3702</td>
<td>1170</td>
<td>9004</td>
</tr>
</table>
 
 
Raw HTML output:
 
<pre><table style="width:25%; border:2px solid silver;">
<caption>Table source generated by JS</caption>
<tr>
<th style="text-align:right;"></th>
<th style="text-align:right;">A</th>
<th style="text-align:right;">B</th>
<th style="text-align:right;">C</th>
</tr>
<tr style="border:1px solid silver;text-align:right;">
<th>1</th>
<td>8464</td>
<td>1650</td>
<td>8275</td>
</tr>
<tr style="border:1px solid silver;text-align:right;">
<th>2</th>
<td>547</td>
<td>9794</td>
<td>8690</td>
</tr>
<tr style="border:1px solid silver;text-align:right;">
<th>3</th>
<td>3702</td>
<td>1170</td>
<td>9004</td>
</tr>
</table>
</pre>
 
=={{header|jq}}==
{{works with|jq|1.4}}
<langsyntaxhighlight lang="jq">def html_row:
"<tr>",
" \(.[] | "<td>\(.)</td>")",
Line 2,238 ⟶ 3,182:
length as $length
| . as $in
| [range(0;length) | [.+1] + $in[.]] | html_table(header);</langsyntaxhighlight>
 
'''Example'''
<langsyntaxhighlight lang="jq">def data:
[ [4,5,6],
[41, 51, 61],
Line 2,247 ⟶ 3,191:
 
# The first column has no header
data | html_table_with_sequence( ["", "X", "Y", "Z"] )</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -r -n -f Create_an_HTML_table.jq
<table>
<thead align = 'right'>
Line 2,279 ⟶ 3,223:
</tr>
</tbody
</table></langsyntaxhighlight>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">function tag(x::Pair, attr::Pair...)
t, b = x
attrstr = join(" $n=\"$p\"" for (n, p) in attr)
return "<$t$attrstr>$b</$t>"
end
 
colnames = split(",X,Y,Z", ',')
 
header = join(tag(:th => txt) for txt in colnames) * "\n"
rows = collect(tag(:tr => join(tag(:td => i, :style => "font-weight: bold;") * join(tag(:td => rand(1000:9999)) for j in 1:3))) for i in 1:6)
body = "\n" * join(rows, '\n') * "\n"
table = tag(:table => string('\n', header, body, '\n'), :style => "width: 60%")
println(table)</syntaxhighlight>
 
{{out}}
<table style="width: 60%">
<th></th><th>X</th><th>Y</th><th>Z</th>
 
<tr><td style="font-weight: bold;">1</td><td>5399</td><td>5770</td><td>3362</td></tr>
<tr><td style="font-weight: bold;">2</td><td>4564</td><td>1577</td><td>5428</td></tr>
<tr><td style="font-weight: bold;">3</td><td>6257</td><td>7290</td><td>6138</td></tr>
<tr><td style="font-weight: bold;">4</td><td>3912</td><td>5163</td><td>2451</td></tr>
<tr><td style="font-weight: bold;">5</td><td>1426</td><td>1874</td><td>3944</td></tr>
<tr><td style="font-weight: bold;">6</td><td>3896</td><td>9827</td><td>7006</td></tr>
 
</table>
 
 
'''The raw html:'''
<syntaxhighlight lang="html5"><table style="width: 60%">
<th></th><th>X</th><th>Y</th><th>Z</th>
 
<tr><td style="font-weight: bold;">1</td><td>5399</td><td>5770</td><td>3362</td></tr>
<tr><td style="font-weight: bold;">2</td><td>4564</td><td>1577</td><td>5428</td></tr>
<tr><td style="font-weight: bold;">3</td><td>6257</td><td>7290</td><td>6138</td></tr>
<tr><td style="font-weight: bold;">4</td><td>3912</td><td>5163</td><td>2451</td></tr>
<tr><td style="font-weight: bold;">5</td><td>1426</td><td>1874</td><td>3944</td></tr>
<tr><td style="font-weight: bold;">6</td><td>3896</td><td>9827</td><td>7006</td></tr>
 
</table></syntaxhighlight>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.3
 
import java.util.Random
 
fun main(args: Array<String>) {
val r = Random()
val sb = StringBuilder()
val i = " " // indent
with (sb) {
append("<html>\n<head>\n")
append("<style>\n")
append("table, th, td { border: 1px solid black; }\n")
append("th, td { text-align: right; }\n")
append("</style>\n</head>\n<body>\n")
append("<table style=\"width:60%\">\n")
append("$i<thead>\n")
append("$i$i<tr><th></th>")
for (c in 'X'..'Z') append("<th>$c</th>")
append("</tr>\n")
append("$i</thead>\n")
append("$i<tbody>\n")
val f = "$i$i<tr><td>%d</td><td>%d</td><td>%d</td><td>%d</td></tr>\n"
for (j in 1..4) {
append(f.format(j, r.nextInt(10000), r.nextInt(10000), r.nextInt(10000)))
}
append("$i</tbody>\n")
append("</table>\n")
append("</body>\n</html>")
}
println(sb.toString())
}</syntaxhighlight>
 
{{out}}
Sample output:
<syntaxhighlight lang="html5"><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>1444</td><td>3451</td><td>2568</td></tr>
<tr><td>2</td><td>876</td><td>3866</td><td>824</td></tr>
<tr><td>3</td><td>2710</td><td>3845</td><td>8089</td></tr>
<tr><td>4</td><td>6480</td><td>7885</td><td>9246</td></tr>
</tbody>
</table>
</body>
</html></syntaxhighlight>
 
=={{header|Lambdatalk}}==
 
Lambdatalk outputs standard HTML/CSS code sent to the web browser who does the job.
 
<syntaxhighlight lang="scheme">
{table
{@ style="background:#ffe; width:50%;"}
{tr {@ style="text-align:right; font:bold 1.0em arial;"}
{td } {td X} {td Y} {td Z}}
{map {lambda {:i}
{tr {td {b :i}}
{map {lambda {_}
{td {@ style="text-align:right; font:italic 1.0em courier;"}
{floor {* {random} 10000}} }}
{serie 1 3}}}}
{serie 1 3}}}
</syntaxhighlight>
 
{{out}}
 
<table style="background:#ffe; width:50%;"><tr style="text-align:right; font:bold 1.0em arial;"><td></td> <td>X</td> <td>Y</td> <td>Z</td></tr> <tr><td><b>1</b></td> <td style="text-align:right; font:italic 1.0em courier;">4220</td> <td style="text-align:right; font:italic 1.0em courier;">7476</td> <td style="text-align:right; font:italic 1.0em courier;">4414</td></tr> <tr><td><b>2</b></td> <td style="text-align:right; font:italic 1.0em courier;">6</td> <td style="text-align:right; font:italic 1.0em courier;">5335</td> <td style="text-align:right; font:italic 1.0em courier;">1381</td></tr> <tr><td><b>3</b></td> <td style="text-align:right; font:italic 1.0em courier;">8549</td> <td style="text-align:right; font:italic 1.0em courier;">1598</td> <td style="text-align:right; font:italic 1.0em courier;">5380</td></tr></table>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">define rand4dig => integer_random(9999, 1)
 
local(
Line 2,302 ⟶ 3,367:
#output -> append('</table>\n')
 
#output</langsyntaxhighlight>
{{out}}
<table border=2 cellpadding=5 cellspace=0>
Line 2,336 ⟶ 3,401:
<br>
A time delay is needed to allow this, then the file is deleted.
<syntaxhighlight lang="lb">
<lang lb>
nomainwin
 
Line 2,377 ⟶ 3,442:
end
end sub
</syntaxhighlight>
</lang>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">on htmlTable (data)
str = "<table>"
 
Line 2,404 ⟶ 3,469:
put "</table>" after str
return str
end</langsyntaxhighlight>
 
<langsyntaxhighlight lang="lingo">tableData = [\
["X", "Y", "Z"],\
["1", "2", "3"],\
Line 2,417 ⟶ 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>"</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight Lualang="lua">function htmlTable (data)
local html = "<table>\n<tr>\n<th></th>\n"
for _, heading in pairs(data[1]) do
Line 2,444 ⟶ 3,509:
}
 
print(htmlTable(tableData))</langsyntaxhighlight>
{{out}}
<pre><table>
Line 2,472 ⟶ 3,537:
</tr>
</table></pre>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="qbasic">
MODULE HtmlTable {
tag$=LAMBDA$ (a$)-> {
=LAMBDA$ a$ -> {
IF ISNUM THEN w$=STR$(NUMBER,0) ELSE w$=LETTER$
READ ? part$
="<"+a$+IF$(LEN(part$)>0->" "+part$,"")+">"+w$+"</"+a$+">"+CHR$(13)+CHR$(10)
}
}
INVENTORY Fun
STACK NEW {
DATA "html", "head", "body", "table", "tr", "th", "td"
WHILE NOT EMPTY
OVER ' duplicate top of stack
APPEND Fun, LETTER$:=tag$(LETTER$)
END WHILE
}
DEF body0$="",body$=""
STACK NEW {
DATA "", "X", "Y", "Z"
FOR i=1 TO 4
body0$+=Fun$("th")(LETTER$)
z$=""
FOR j=1 TO 3 : z$+=Fun$("td")(RANDOM(0, 9999), {align="right"}) : NEXT j
body$+=Fun$("tr")(Fun$("th")(i)+z$)
NEXT i
}
table$=fun$("table")(fun$("tr")(body0$)+body$,"border=1 cellpadding=10 cellspacing=0")
DOCUMENT final$="<!DOCTYPE html>"+CHR$(13)+CHR$(10)
final$=fun$("html")(fun$("head")("")+fun$("body")(table$), {lang="en"})
file$="c:\doc.html"
REPORT final$
CLIPBOARD final$
SAVE.DOC final$, file$
WIN file$ ' execute, no wait
}
HtmlTable
</syntaxhighlight>
 
{{out}}
<pre style="height:30ex;overflow:scroll">
<!DOCTYPE html>
<html lang="en"><head></head>
<body><table border=1 cellpadding=10 cellspacing=0><tr><th></th>
<th>X</th>
<th>Y</th>
<th>Z</th>
</tr>
<tr><th>1</th>
<td align="right">9007</td>
<td align="right">1425</td>
<td align="right">4897</td>
</tr>
<tr><th>2</th>
<td align="right">1795</td>
<td align="right">6858</td>
<td align="right">9682</td>
</tr>
<tr><th>3</th>
<td align="right">2233</td>
<td align="right">8270</td>
<td align="right">5221</td>
</tr>
<tr><th>4</th>
<td align="right">6619</td>
<td align="right">3688</td>
<td align="right">4052</td>
</tr>
</table>
</body>
</html>
</pre >
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">x := RandomInteger[10];
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>"]</langsyntaxhighlight>
 
{{out}}
Line 2,489 ⟶ 3,628:
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight lang="matlab">function htmltable(fid,table,Label)
fprintf(fid,'<table>\n <thead align = "right">\n');
if nargin<3,
Line 2,500 ⟶ 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</langsyntaxhighlight>
 
{{out}}
Line 2,518 ⟶ 3,657:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE testCGI;
 
FROM InOut IMPORT WriteCard, WriteLn, WriteString, WriteBf;
Line 2,564 ⟶ 3,703:
WriteLn;
WriteBf
END testCGI.</langsyntaxhighlight>
 
=={{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.
<syntaxhighlight lang="nanoquery">import Nanoquery.Util
 
random = new(Random)
 
println "<table>"
 
// generate header
println "<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>"
 
// generate five rows
for i in range(1, 5)
println "<tr><td style=\"font-weight: bold;\">" + i + "</td>"
println "<td>" + int($random.getInt(8999) + 1000) + "</td>"
println "<td>" + int($random.getInt(8999) + 1000) + "</td>"
println "<td>" + int($random.getInt(8999) + 1000) + "</td>"
println "</tr>"
end for
 
println "</table>"</syntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,689 ⟶ 3,874:
return html
 
</syntaxhighlight>
</lang>
{{out}}
<div style="height:80ex;overflow:scroll;">
<langsyntaxhighlight lang="html5"><?xml version="1.0" encoding="UTF-8"?>
<!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 2,760 ⟶ 3,945:
</body>
</html>
</syntaxhighlight>
</lang>
</div>
{{out}}
Line 2,766 ⟶ 3,951:
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">; file: html-table.lsp
; url: http://rosettacode.org/wiki/Create_an_HTML_table
; author: oofoe 2012-01-29
Line 2,821 ⟶ 4,006:
))
 
(exit)</langsyntaxhighlight>
 
{{out}}
Line 2,881 ⟶ 4,066:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import mathrandom, htmlgen
randomize()
 
template randTD(): exprstring = td($randomrand(1000..9999))
proc randTR(x: int): autostring =
tr(td($x, style="font-weight: bold"), randTD, randTD, randTD)
 
Line 2,894 ⟶ 4,079:
randTR 3,
randTR 4,
randTR 5)</langsyntaxhighlight>
{{out}}
<table><tr><th></th><th>X</th><th>Y</th><th>Z</th></tr><tr><td style="font-weight: bold">1</td><td>50848815</td><td>70595115</td><td>83089452</td></tr><tr><td style="font-weight: bold">2</td><td>61855048</td><td>35495694</td><td>88316610</td></tr><tr><td style="font-weight: bold">3</td><td>80636163</td><td>55614598</td><td>16759015</td></tr><tr><td style="font-weight: bold">4</td><td>27775638</td><td>17698679</td><td>65703994</td></tr><tr><td style="font-weight: bold">5</td><td>54651515</td><td>95081822</td><td>47754020</td></tr></table>
 
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>50848815</td><td>70595115</td><td>83089452</td></tr><tr><td style="font-weight: bold">2</td><td>61855048</td><td>35495694</td><td>88316610</td></tr><tr><td style="font-weight: bold">3</td><td>80636163</td><td>55614598</td><td>16759015</td></tr><tr><td style="font-weight: bold">4</td><td>27775638</td><td>17698679</td><td>65703994</td></tr><tr><td style="font-weight: bold">5</td><td>54651515</td><td>95081822</td><td>47754020</td></tr></table></pre>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
class CreateTable {
function : Main(args : String[]) ~ Nil {
Line 2,936 ⟶ 4,121:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
Line 2,942 ⟶ 4,127:
A simple <code>printf</code> method:
 
<langsyntaxhighlight lang="ocaml">let () =
let buf = Buffer.create 1 in
let s = Buffer.add_string buf in
Line 2,964 ⟶ 4,149:
s "</tbody>";
s "</table>";
print_endline (Buffer.contents buf)</langsyntaxhighlight>
 
=== With a dedicated library ===
Line 2,970 ⟶ 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.
 
<langsyntaxhighlight lang="ocaml">open XHTML.M_01_01
 
let _td s = td [pcdata s]
Line 3,008 ⟶ 4,193:
 
let () =
pretty_print ~width:80 print_string my_page</langsyntaxhighlight>
 
=== TyXml ===
Line 3,014 ⟶ 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.
 
<langsyntaxhighlight lang="ocaml">#use "topfind"
#require "tyxml"
 
Line 3,038 ⟶ 4,223:
let my_html = X.html my_head my_body in
P.print print_endline my_html;
;;</langsyntaxhighlight>
 
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>:
 
<langsyntaxhighlight lang="ocaml">let make_table () =
let br = X.a_border 1 in
let th s = X.th [X.pcdata s] in
Line 3,052 ⟶ 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)</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="oz">declare
 
[Roads] = {Module.link ['x-ozlib://wmeyer/roads/Roads.ozf']}
Line 3,096 ⟶ 4,281:
 
{Roads.registerFunction table Table}
{Roads.run}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">html(n=3)={
print("<table>\n<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>");
for(i=1,n,
Line 3,107 ⟶ 4,292:
);
print("</table>")
};</langsyntaxhighlight>
{{out}}
<pre><table>
Line 3,117 ⟶ 4,302:
 
Note also the built-in <code>printtex</code> command, which allows the analogous task to be written as
<langsyntaxhighlight lang="parigp">printtex(matrix(4,4,i,j,if(i==1,if(j==1,"",Strchr(86+j)),if(j==1,i,random(9999)))))</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 3,125 ⟶ 4,310:
Opcodes of interest: SDC -- simple document; R!I -- ranged random integer
 
<langsyntaxhighlight lang="sgml"><@ SDCLIT>
<@ DTBLIT>
<@ DTRLITLIT>
Line 3,141 ⟶ 4,326:
</@>
</@>
|Number Table</@></langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">my @heading = qw(X Y Z);
my $rows = 5;
print '<table><thead><td>',
Line 3,156 ⟶ 4,341:
}
 
print "</tbody></table>";</langsyntaxhighlight>
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|Perl 6}}==
 
This is certainly not the only or best way to generate HTML tables using Perl 6; just an example of one possible method.
 
<lang Perl6>my @header = <&nbsp; X Y Z>;
my $rows = 5;
 
sub tag ($tag, $string, $param?) { return "<$tag" ~ ($param ?? " $param" !! '') ~ ">$string" ~ "</$tag>" };
 
my $table = tag('tr', ( tag('th', $_) for @header));
 
for 1 .. $rows -> $row {
$table ~= tag('tr', ( tag('td', $row, 'align="right"')
~ (tag('td', (^10000).pick, 'align="right"') for 1..^@header)));
}
 
say tag('table', $table, 'cellspacing=4 style="text-align:right; border: 1px solid;"');</lang>
 
{{out}}
<table cellspacing=4 style="text-align:right; border: 1px solid;"><tr><th>&nbsp;</th> <th>X</th> <th>Y</th> <th>Z</th></tr><tr><td align="right">1</td><td align="right">2179</td> <td align="right">4778</td> <td align="right">2717</td></tr><tr><td align="right">2</td><td align="right">2160</td> <td align="right">1592</td> <td align="right">4348</td></tr><tr><td align="right">3</td><td align="right">4511</td> <td align="right">540</td> <td align="right">7187</td></tr><tr><td align="right">4</td><td align="right">3484</td> <td align="right">5882</td> <td align="right">1273</td></tr><tr><td align="right">5</td><td align="right">1310</td> <td align="right">4017</td> <td align="right">410</td></tr></table>
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
<lang Phix>puts(1,"<table>\n")
===plain text approach===
puts(1," <tr><th></th>")
<!--<syntaxhighlight lang="phix">-->
for j=1 to 3 do
<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;">"&lt;table border=2&gt;\n"</span><span style="color: #0000FF;">)</span>
printf(1,"<th>%s</th>",'W'+j)
<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;">" &lt;tr&gt;&lt;th&gt;&lt;/th&gt;"</span><span style="color: #0000FF;">)</span>
end for
<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>
puts(1,"</tr>\n")
<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;">"&lt;th&gt;%s&lt;/th&gt;"</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>
for i=1 to 3 do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1," <tr><td>%d</td>",i)
<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;">"&lt;/tr&gt;\n"</span><span style="color: #0000FF;">)</span>
for j=1 to 3 do
<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>
printf(1,"<td>%d</td>",rand(10000))
<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;">" &lt;tr&gt;&lt;td&gt;%d&lt;/td&gt;"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
end for
<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>
puts(1,"</tr>\n")
<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;">"&lt;td&gt;%d&lt;/td&gt;"</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>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
puts(1,"</table>")</lang>
<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;">"&lt;/tr&gt;\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;">"&lt;/table&gt;"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<table border=2>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td><td>3287</td><td>6480</td><td>6510</td></tr>
Line 3,203 ⟶ 4,370:
<tr><td>3</td><td>3287</td><td>6600</td><td>3953</td></tr>
</table>
 
 
'''The raw HTML'''
<pre>
<lang html5><table>
<&lt;table> border=2&gt;
<&lt;tr><&gt;&lt;th><&gt;&lt;/th><&gt;&lt;th>&gt;X<&lt;/th><&gt;&lt;th>&gt;Y<&lt;/th><&gt;&lt;th>&gt;Z<&lt;/th><&gt;&lt;/tr>&gt;
<&lt;tr><&gt;&lt;td>&gt;1<&lt;/td><&gt;&lt;td>&gt;3287<&lt;/td><&gt;&lt;td>&gt;6480<&lt;/td><&gt;&lt;td>&gt;6510<&lt;/td><&gt;&lt;/tr>&gt;
<&lt;tr><&gt;&lt;td>&gt;2<&lt;/td><&gt;&lt;td>&gt;8500<&lt;/td><&gt;&lt;td>&gt;1908<&lt;/td><&gt;&lt;td>&gt;5352<&lt;/td><&gt;&lt;/tr>&gt;
<&lt;tr><&gt;&lt;td>&gt;3<&lt;/td><&gt;&lt;td>&gt;3287<&lt;/td><&gt;&lt;td>&gt;6600<&lt;/td><&gt;&lt;td>&gt;3953<&lt;/td><&gt;&lt;/tr>&gt;
<&lt;/table>&gt;</langpre>
 
===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">
<tr>
<th style="text-align:right; padding: 5px;" />
<th style="text-align:right; padding: 5px;">X</th>
<th style="text-align:right; padding: 5px;">Y</th>
<th style="text-align:right; padding: 5px;">Z</th>
</tr>
<tr>
<td style="text-align:right; padding: 5px;">1</td>
<td style="text-align:right; padding: 5px;">7923</td>
<td style="text-align:right; padding: 5px;">3194</td>
<td style="text-align:right; padding: 5px;">3339</td>
</tr>
<tr>
<td style="text-align:right; padding: 5px;">2</td>
<td style="text-align:right; padding: 5px;">7782</td>
<td style="text-align:right; padding: 5px;">6177</td>
<td style="text-align:right; padding: 5px;">8690</td>
</tr>
<tr>
<td style="text-align:right; padding: 5px;">3</td>
<td style="text-align:right; padding: 5px;">1474</td>
<td style="text-align:right; padding: 5px;">7271</td>
<td style="text-align:right; padding: 5px;">8167</td>
</tr>
</table>
 
 
'''The raw XML'''
<pre>
&lt;table border="2"&gt;
&lt;tr&gt;
&lt;th style="text-align:right; padding: 5px;" /&gt;
&lt;th style="text-align:right; padding: 5px;"&gt;X&lt;/th&gt;
&lt;th style="text-align:right; padding: 5px;"&gt;Y&lt;/th&gt;
&lt;th style="text-align:right; padding: 5px;"&gt;Z&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align:right; padding: 5px;"&gt;1&lt;/td&gt;
&lt;td style="text-align:right; padding: 5px;"&gt;7923&lt;/td&gt;
&lt;td style="text-align:right; padding: 5px;"&gt;3194&lt;/td&gt;
&lt;td style="text-align:right; padding: 5px;"&gt;3339&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align:right; padding: 5px;"&gt;2&lt;/td&gt;
&lt;td style="text-align:right; padding: 5px;"&gt;7782&lt;/td&gt;
&lt;td style="text-align:right; padding: 5px;"&gt;6177&lt;/td&gt;
&lt;td style="text-align:right; padding: 5px;"&gt;8690&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align:right; padding: 5px;"&gt;3&lt;/td&gt;
&lt;td style="text-align:right; padding: 5px;"&gt;1474&lt;/td&gt;
&lt;td style="text-align:right; padding: 5px;"&gt;7271&lt;/td&gt;
&lt;td style="text-align:right; padding: 5px;"&gt;8167&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;</pre>
 
=={{header|PHP}}==
 
=== normal style ===
<langsyntaxhighlight PHPlang="php"><?php
/**
* @author Elad Yosifon
Line 3,252 ⟶ 4,500:
 
echo $html;
</syntaxhighlight>
</lang>
 
=== template engine style ===
<langsyntaxhighlight PHPlang="php"><?php
/**
* @author Elad Yosifon
Line 3,290 ⟶ 4,538:
</body>
</html>
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/xhtml.l")
 
(<table> NIL NIL '(NIL (NIL "X") (NIL "Y") (NIL "Z"))
(for N 3
(<row> NIL N 124 456 789) ) )</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
/* Create an HTML table. 6/2011 */
 
Line 3,344 ⟶ 4,592:
call create_table (headings, table_contents);
 
end create;</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
# Converts Microsoft .NET Framework objects into HTML that can be displayed in a Web browser.
ConvertTo-Html -inputobject (Get-Date)
Line 3,358 ⟶ 4,606:
 
$object | ConvertTo-Html
</syntaxhighlight>
</lang>
{{out}}
<syntaxhighlight lang="powershell">
<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 3,386 ⟶ 4,634:
</table>
</body></html>
</syntaxhighlight>
</lang>
 
When raw results are exported to Out-File cmdlet like this:
<syntaxhighlight lang="powershell">
<lang PowerShell>
$object | ConvertTo-Html | Out-File -FilePath $env:temp\test.html ; invoke-item $env:temp\test.html
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,399 ⟶ 4,647:
</table>
 
=={{header|PythonProlog}}==
<syntaxhighlight lang="prolog">
<lang python>
:- use_module(library(http/html_write)).
import random
 
theader([]) --> []. theader([H|T]) --> html(th(H)), theader(T).
def rand9999():
trows([],_) --> []. trows([R|T], N) --> html(tr([td(N),\trow(R)])), { N1 is N + 1 }, trows(T, N1).
return random.randint(1000, 9999)
trow([]) --> []. trow([E|T]) --> html(td(E)), trow(T).
 
table :-
def tag(attr='', **kwargs):
Header = ['X','Y','Z'],
for tag, txt in kwargs.items():
Rows = [
return '<{tag}{attr}>{txt}</{tag}>'.format(**locals())
[7055,5334,5795],
[2895,3019,7747],
[140,7607,8144],
[7090,475,4140]
],
phrase(html(table([tr(\theader(Header)), \trows(Rows,1)])), Out, []),
print_html(Out).</syntaxhighlight>
 
<syntaxhighlight lang="html5"><table>
if __name__ == '__main__':
<tr><th>X</th><th>Y</th><th>Z</th></tr>
header = tag(tr=''.join(tag(th=txt) for txt in ',X,Y,Z'.split(','))) + '\n'
<tr><td>1</td><td>7055</td><td>5334</td><td>5795</td></tr>
rows = '\n'.join(tag(tr=''.join(tag(' style="font-weight: bold;"', td=i)
<tr><td>2</td><td>2895</td><td>3019</td><td>7747</td></tr>
+ ''.join(tag(td=rand9999())
<tr><td>3</td><td>140</td><td>7607</td><td>8144</td></tr>
for j in range(3))))
<tr><td>4</td><td>7090</td><td>475</td><td>4140</td></tr>
for i in range(1, 6))
</table></syntaxhighlight>
table = tag(table='\n' + header + rows + '\n')
print(table)</lang>
 
{{out}}
<table>
<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>
<tr><td style="font-weight: bold;">2</td><td>2525</td><td>5468</td><td>6901</td></tr>
<tr><td style="font-weight: bold;">3</td><td>8851</td><td>3727</td><td>8379</td></tr>
<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>
 
'''The raw HTML'''
<lang html5><table>
<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>
<tr><td style="font-weight: bold;">2</td><td>2525</td><td>5468</td><td>6901</td></tr>
<tr><td style="font-weight: bold;">3</td><td>8851</td><td>3727</td><td>8379</td></tr>
<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></lang>
 
=={{header|PureBasic}}==
Line 3,444 ⟶ 4,679:
</font>
 
<syntaxhighlight lang="purebasic">
<lang PureBasic>
Title.s="Create an HTML table"
 
Line 3,485 ⟶ 4,720:
 
; RunProgram(FileName.s)
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,499 ⟶ 4,734:
'''The raw HTML'''
 
<langsyntaxhighlight lang="html5">
<html><head><title>Create an HTML table</title></head><body>
<table border=1 cellpadding=10 cellspacing=0>
Line 3,509 ⟶ 4,744:
</table>
</body></html>
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
<syntaxhighlight lang="python">
import random
 
def rand9999():
return random.randint(1000, 9999)
 
def tag(attr='', **kwargs):
for tag, txt in kwargs.items():
return '<{tag}{attr}>{txt}</{tag}>'.format(**locals())
 
if __name__ == '__main__':
header = tag(tr=''.join(tag(th=txt) for txt in ',X,Y,Z'.split(','))) + '\n'
rows = '\n'.join(tag(tr=tag(' style="font-weight: bold;"', td=i)
+ ''.join(tag(td=rand9999())
for j in range(3)))
for i in range(1, 6))
table = tag(table='\n' + header + rows + '\n')
print(table)</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>6040</td><td>4697</td><td>7055</td></tr>
<tr><td style="font-weight: bold;">2</td><td>2525</td><td>5468</td><td>6901</td></tr>
<tr><td style="font-weight: bold;">3</td><td>8851</td><td>3727</td><td>8379</td></tr>
<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>
 
'''The raw HTML'''
<syntaxhighlight lang="html5"><table>
<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>
<tr><td style="font-weight: bold;">2</td><td>2525</td><td>5468</td><td>6901</td></tr>
<tr><td style="font-weight: bold;">3</td><td>8851</td><td>3727</td><td>8379</td></tr>
<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></syntaxhighlight>
 
 
Or, folding a general HTML render function over a tree of data:
 
{{Trans|JavaScript}}
{{works with|Python|3.6}}
<syntaxhighlight lang="python">from functools import (reduce)
import itertools
import random
 
 
# HTML RENDERING ----------------------------------------
 
# treeHTML :: tree
# {tag :: String, text :: String, kvs :: Dict}
# -> HTML String
def treeHTML(tree):
return foldTree(
lambda x: lambda xs: (
f"<{x['tag'] + attribString(x)}>" + (
str(x['text']) if 'text' in x else '\n'
) + ''.join(xs) + f"</{x['tag']}>\n"
)
)(tree)
 
 
# attribString :: Dict -> String
def attribString(dct):
kvs = dct['kvs'] if 'kvs' in dct else None
return ' ' + reduce(
lambda a, k: a + k + '="' + kvs[k] + '" ',
kvs.keys(), ''
).strip() if kvs else ''
 
 
# HTML TABLE FROM GENERATED DATA ------------------------
 
 
def main():
# Number of columns and rows to generate.
n = 3
 
# Table details -------------------------------------
strCaption = 'Table generated with Python'
colNames = take(n)(enumFrom('A'))
dataRows = map(
lambda x: (x, map(
lambda _: random.randint(100, 9999),
colNames
)), take(n)(enumFrom(1)))
tableStyle = {
'style': "width:25%; border:2px solid silver;"
}
trStyle = {
'style': "border:1px solid silver;text-align:right;"
}
 
# TREE STRUCTURE OF TABLE ---------------------------
tableTree = Node({'tag': 'table', 'kvs': tableStyle})([
Node({
'tag': 'caption',
'text': strCaption
})([]),
 
# HEADER ROW --------------------------------
(Node({'tag': 'tr'})(
Node({
'tag': 'th',
'kvs': {'style': 'text-align:right;'},
'text': k
})([]) for k in ([''] + colNames)
))
] +
# DATA ROWS ---------------------------------
list(Node({'tag': 'tr', 'kvs': trStyle})(
[Node({'tag': 'th', 'text': tpl[0]})([])] +
list(Node(
{'tag': 'td', 'text': str(v)})([]) for v in tpl[1]
)
) for tpl in dataRows)
)
 
print(
treeHTML(tableTree)
# dataRows
)
 
 
# GENERIC -----------------------------------------------
 
# Node :: a -> [Tree a] -> Tree a
def Node(v):
return lambda xs: {'type': 'Node', 'root': v, 'nest': xs}
 
 
# enumFrom :: Enum a => a -> [a]
def enumFrom(x):
return itertools.count(x) if type(x) is int else (
map(chr, itertools.count(ord(x)))
)
 
 
# foldTree :: (a -> [b] -> b) -> Tree a -> b
def foldTree(f):
def go(node):
return f(node['root'])(
list(map(go, node['nest']))
)
return lambda tree: go(tree)
 
 
# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
def take(n):
return lambda xs: (
xs[0:n]
if isinstance(xs, list)
else list(itertools.islice(xs, n))
)
 
 
if __name__ == '__main__':
main()</syntaxhighlight>
 
{{Out}}
<table style="width:25%; border:2px solid silver;">
<caption>Table generated with Python</caption>
<tr>
<th style="text-align:right;"></th>
<th style="text-align:right;">A</th>
<th style="text-align:right;">B</th>
<th style="text-align:right;">C</th>
</tr>
<tr style="border:1px solid silver;text-align:right;">
<th>1</th>
<td>7469</td>
<td>7407</td>
<td>6448</td>
</tr>
<tr style="border:1px solid silver;text-align:right;">
<th>2</th>
<td>4135</td>
<td>3299</td>
<td>6586</td>
</tr>
<tr style="border:1px solid silver;text-align:right;">
<th>3</th>
<td>8264</td>
<td>6951</td>
<td>8882</td>
</tr>
</table>
 
 
Raw HTML output
<syntaxhighlight lang="html5"><table style="width:25%; border:2px solid silver;">
<caption>Table generated with Python</caption>
<tr>
<th style="text-align:right;"></th>
<th style="text-align:right;">A</th>
<th style="text-align:right;">B</th>
<th style="text-align:right;">C</th>
</tr>
<tr style="border:1px solid silver;text-align:right;">
<th>1</th>
<td>7469</td>
<td>7407</td>
<td>6448</td>
</tr>
<tr style="border:1px solid silver;text-align:right;">
<th>2</th>
<td>4135</td>
<td>3299</td>
<td>6586</td>
</tr>
<tr style="border:1px solid silver;text-align:right;">
<th>3</th>
<td>8264</td>
<td>6951</td>
<td>8882</td>
</tr>
</table></syntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 3,531 ⟶ 4,988:
 
(display-xml/content (xexpr->xml xexpr))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
The below example is kind of boring, and laughably simple. For more interesting/complicated examples see:
 
[[Show_Ascii_table#Raku]] - ''(simple)''<br>
[[Mayan_numerals#Raku]] - ''(heavy styling)''<br>
[[Rosetta_Code/Count_examples/Full_list]] - ''(multi-column sortable)''<br>
[[Rosetta_Code/List_authors_of_task_descriptions/Full_list]] - ''(complex nested tables)''
 
''Note: the above examples are outputting wikitable formatting, not HTML directly. It's pretty much a one-for-one shorthand notation though and the principles and process are the same.
 
 
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" line>my @header = <&nbsp; X Y Z>;
my $rows = 5;
 
sub tag ($tag, $string, $param?) { return "<$tag" ~ ($param ?? " $param" !! '') ~ ">$string" ~ "</$tag>" };
 
my $table = tag('tr', ( tag('th', $_) for @header));
 
for 1 .. $rows -> $row {
$table ~= tag('tr', ( tag('td', $row, 'align="right"')
~ (tag('td', (^10000).pick, 'align="right"') for 1..^@header)));
}
 
say tag('table', $table, 'cellspacing=4 style="text-align:right; border: 1px solid;"');</syntaxhighlight>
 
{{out}}
<table cellspacing=4 style="text-align:right; border: 1px solid;"><tr><th>&nbsp;</th> <th>X</th> <th>Y</th> <th>Z</th></tr><tr><td align="right">1</td><td align="right">2179</td> <td align="right">4778</td> <td align="right">2717</td></tr><tr><td align="right">2</td><td align="right">2160</td> <td align="right">1592</td> <td align="right">4348</td></tr><tr><td align="right">3</td><td align="right">4511</td> <td align="right">540</td> <td align="right">7187</td></tr><tr><td align="right">4</td><td align="right">3484</td> <td align="right">5882</td> <td align="right">1273</td></tr><tr><td align="right">5</td><td align="right">1310</td> <td align="right">4017</td> <td align="right">410</td></tr></table>
 
=={{header|Rascal}}==
<langsyntaxhighlight lang="rascal">import IO;
import util::Math;
 
Line 3,551 ⟶ 5,040:
html("Rosetta Code Table", table(rows)));
return "written";
}</langsyntaxhighlight>
 
This will result in a simple html file. For example:
 
<langsyntaxhighlight lang="rascal">rascal>generateTable(10)
str: "written"</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="html"><html><title>Rosetta Code Table</title><body><table border="0"><tr><td></td><td>X</td><td>Y</td><td>Z</td></tr><tr><td>1</td><td>253</td><td>3988</td><td>3208</td></tr><tr><td>2</td><td>315</td><td>2014</td><td>47</td></tr><tr><td>3</td><td>749</td><td>3379</td><td>1076</td></tr><tr><td>4</td><td>241</td><td>3211</td><td>1848</td></tr><tr><td>5</td><td>1</td><td>1605</td><td>6469</td></tr><tr><td>6</td><td>599</td><td>1243</td><td>1189</td></tr><tr><td>7</td><td>741</td><td>4709</td><td>2854</td></tr><tr><td>8</td><td>918</td><td>482</td><td>7160</td></tr><tr><td>9</td><td>451</td><td>572</td><td>6229</td></tr><tr><td>10</td><td>955</td><td>7970</td><td>9684</td></tr></table border="0"></body></html></langsyntaxhighlight>
 
{{out}}
[[File:Tableoutput.JPG]]
 
=={{header|Red}}==
<syntaxhighlight lang="red">
 
Create an HTML table
 
The table body should have at least three rows of three columns.
Each of these three columns should be labelled "X", "Y", and "Z".
An extra column should be added at either the extreme left or the extreme right
of the table that has no heading, but is filled with sequential row numbers.
The rows of the "X", "Y", and "Z" columns should be filled with random or
sequential integers having 4 digits or less.
The numbers should be aligned in the same fashion for all columns.
 
Red [
Problem: %http://www.rosettacode.org/wiki/Create_an_HTML_table
Code: %https://github.com/metaperl/red-rosetta/blob/master/html-table.r
Acknowledgements: "@endo64 @toomsav"
]
 
result: func[][simple-tag "table" trs]
 
trs: func [][rejoin [
first-tr
rand-tr 1
rand-tr 2
rand-tr 3
rand-tr 4
rand-tr 5
]]
 
table-data: func [][999 + (random 9000)]
rand-td: func [][simple-tag "td" table-data]
rand-tr: func [i][rejoin [
simple-tag "tr"
rejoin [(simple-tag "td" i) rand-td rand-td rand-td]
]]
first-tr: func[][rejoin [
simple-tag "tr" rejoin [
simple-tag "th" ""
simple-tag "th" "X"
simple-tag "th" "Y"
simple-tag "th" "Z"
]
]]
 
simple-tag: func [tag contents /attr a][rejoin
["<" tag (either attr [rejoin [" " a/1 "=" {"} a/2 {"}]][]) ">"
newline contents newline "</" tag ">"]]
 
</syntaxhighlight>
 
=={{header|Retro}}==
Using the '''casket::html'''' library which allows creation of HTML using quotes and combinators:
 
<langsyntaxhighlight Retrolang="retro">needs casket::html'
with casket::html'
 
Line 3,579 ⟶ 5,119:
[ [ "5" ] td [ rnd ] td [ rnd ] td [ rnd ] td ] tr
[ [ "6" ] td [ rnd ] td [ rnd ] td [ rnd ] td ] tr
] table</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 3,585 ⟶ 5,125:
<br>makes reading the file easier and also helps in debugging, &nbsp; but they could've been combined into a
<br>single &nbsp; '''lineout''' &nbsp; for succinctness.
<langsyntaxhighlight lang="rexx">/*REXX program creates (and displays) an HTML table of five rows and three columns.*/
parse arg rows . /*obtain optional argument from the CL.*/
if rows=='' | rows=="," then rows=5 /*no ROWS specified? Then use default.*/
Line 3,616 ⟶ 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.*/</langsyntaxhighlight>
'''{{out|output''' |text=&nbsp; to the terminal (screen) using the default input of &nbsp; <tt> 5 </tt> &nbsp; rows:}}
<pre>
══► <html>
Line 3,653 ⟶ 5,193:
31 records were written to the output file: a_table.html
</pre>
 
'''output''' &nbsp; generated &nbsp; (written to the &nbsp; '''a_table.html''' &nbsp; output file):
{{out|output|text=&nbsp; generated &nbsp; (written to the &nbsp; '''a_table.html''' &nbsp; output file):}}
<pre style="height:50ex">
<pre>
<html>
<head></head>
Line 3,687 ⟶ 5,228:
</html>
</pre>
 
{{out}}
{{out|output|text=:}}
<br><br>
<table border=5 cellpadding=20 cellspace=0>
<tr><th></th>
Line 3,715 ⟶ 5,256:
<td align=right> 642 </td>
</table>
<br><br>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project: Create an HTML table
load "stdlib.ring"
 
str = ""
ncols = 3
nrows = 4
str = str + "<html><head></head><body>" + windowsnl()
str = str + "<table border=1 cellpadding=10 cellspacing=0>" + windowsnl()
for row = 0 to nrows
if row = 0
str = str + "<tr><th></th>"
else
str = str + "<tr><th>" + row + "</th>"
ok
for col = 1 to ncols
if row = 0
str = str + "<th>" + char(87 + col) + "</th>"
else
str = str + "<td align=" + '"right"' + ">" + random(9999) + "</td>"
ok
next
str = str + windowsnl() + "</tr>" +windowsnl()
next
str = str + "</table>" + windowsnl()
str = str + "</body></html>" + windowsnl()
 
remove("temp.htm")
write("temp.htm",str)
see str + nl
systemcmd("temp.htm")
</syntaxhighlight>
Output:
<pre>
<html><head></head><body>
<table border=1 cellpadding=10 cellspacing=0>
<tr><th></th><th>X</th><th>Y</th><th>Z</th>
</tr>
<tr><th>1</th><td align="right">9151</td><td align="right">1873</td><td align="right">8011</td>
</tr>
<tr><th>2</th><td align="right">9034</td><td align="right">4211</td><td align="right">7883</td>
</tr>
<tr><th>3</th><td align="right">5983</td><td align="right">3772</td><td align="right">501</td>
</tr>
<tr><th>4</th><td align="right">3468</td><td align="right">3931</td><td align="right">3891</td>
</tr>
</table>
</body></html>
</pre>
 
Output image:
 
[https://www.dropbox.com/s/1c8svjprod2tqyu/CalmoSoftHTTP.jpg?dl=0 Create an HTML table]
 
=={{header|Ruby}}==
Line 3,721 ⟶ 5,321:
Pure Ruby solution:
 
<langsyntaxhighlight lang="ruby">
def r
rand(10000)
Line 3,744 ⟶ 5,344:
html << "</table>"
end
</syntaxhighlight>
</lang>
 
 
Line 3,750 ⟶ 5,350:
 
{{libheader|REXML}}
<langsyntaxhighlight lang="ruby">def r; rand(10000); end
table = [["", "X", "Y", "Z"],
[ 1, r, r, r],
Line 3,769 ⟶ 5,369:
formatter = REXML::Formatters::Pretty.new
formatter.compact = true
formatter.write(xtable, $stdout)</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="html5"><table>
<tr>
<td></td>
Line 3,797 ⟶ 5,397:
<td>9849</td>
</tr>
</table></langsyntaxhighlight>
 
<table>
<tr>
<td></td>
<td>X</td>
<td>Y</td>
<td>Z</td>
</tr>
<tr>
<td>1</td>
<td>1358</td>
<td>6488</td>
<td>6434</td>
</tr>
<tr>
<td>2</td>
<td>2477</td>
<td>6493</td>
<td>1330</td>
</tr>
<tr>
<td>3</td>
<td>240</td>
<td>3038</td>
<td>9849</td>
</tr>
</table>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight RunBasiclang="runbasic">html "<table border=1><tr align=center><td>Row</td><td>X</td><td>Y</td><td>Z</td></tr>"
for i = 1 to 5
html "<tr align=right>"
Line 3,808 ⟶ 5,435:
html "</tr>"
next i
html "</table>"</langsyntaxhighlight>
 
{{out}}
Line 3,818 ⟶ 5,445:
<tr align=right><td>5</td><td>52</td><td>53</td><td>54</td></tr>
</table>
 
=={{header|Rust}}==
{{libheader|rand|0.3}}
<syntaxhighlight lang="rust">extern crate rand;
 
use rand::Rng;
 
fn random_cell<R: Rng>(rng: &mut R) -> u32 {
// Anything between 0 and 10_000 (exclusive) has 4 digits or fewer. Using `gen_range::<u32>`
// is faster for smaller RNGs. Because the parameters are constant, the compiler can do all
// the range construction at compile time, removing the need for
// `rand::distributions::range::Range`
rng.gen_range(0, 10_000)
}
 
fn main() {
let mut rng = rand::thread_rng(); // Cache the RNG for reuse
 
println!("<table><thead><tr><th></th><td>X</td><td>Y</td><td>Z</td></tr></thead>");
 
for row in 0..3 {
let x = random_cell(&mut rng);
let y = random_cell(&mut rng);
let z = random_cell(&mut rng);
println!("<tr><th>{}</th><td>{}</td><td>{}</td><td>{}</td></tr>", row, x, y, z);
}
 
println!("</table>");
}</syntaxhighlight>
 
{{out}}
<syntaxhighlight lang="html5"><table><thead><tr><th></th><td>X</td><td>Y</td><td>Z</td></tr></thead>
<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></syntaxhighlight>
 
=={{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.<langsyntaxhighlight lang="scala">object TableGenerator extends App {
val data = List(List("X", "Y", "Z"), List(11, 12, 13), List(12, 22, 23), List(13, 32, 33))
 
Line 3,836 ⟶ 5,499:
 
println(generateTable(data))
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
{{works with|Guile}}{{works with|Gauche}}<langsyntaxhighlight lang="scheme">(define table #(
#("" "X" "Y" "Z")
#(1 1 2 3)
Line 3,859 ⟶ 5,522:
(display "</td>")))
(display "</tr>"))
(display "</table>")</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 3,879 ⟶ 5,542:
end for;
writeln("</table>")
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,900 ⟶ 5,563:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">class HTML {
method _attr(Hash h) {
h.keys.sort.map {|k| %Q' #{k}="#{h{k}}"' }.join('')
Line 3,949 ⟶ 5,612:
)
}...
);</langsyntaxhighlight>
 
{{out}}
Line 3,988 ⟶ 5,651:
</pre>
(tidied afterwards)
 
=={{header|Snobol4}}==
<syntaxhighlight lang="snobol4">* HTML Table
output = "<table>"
output = " <tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>"
i = 1
o1 output = "<tr><td>" i "</td>"
j = 1
o2 output = "<td>" i j "</td>"
j = lt(j,3) j + 1 :s(o2)
output = "</tr>"
i = lt(i,3) i + 1 :s(o1)
output = "</table>"
end
 
</syntaxhighlight>
{{out}}
<pre><table>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td>
<td>11</td>
<td>12</td>
<td>13</td>
</tr>
<tr><td>2</td>
<td>21</td>
<td>22</td>
<td>23</td>
</tr>
<tr><td>3</td>
<td>31</td>
<td>32</td>
<td>33</td>
</tr>
</table>
</pre>
 
=={{header|Standard ML}}==
Line 3,999 ⟶ 5,698:
 
 
<langsyntaxhighlight lang="sml">(*
* val mkHtmlTable : ('a list * 'b list) -> ('a -> string * 'b -> string)
* -> (('a * 'b) -> string) -> string
Line 4,032 ⟶ 5,731:
(fn (a, b) => Real.toString (Math.pow (a, b))))
 
val _ = print (samplePage ())</langsyntaxhighlight>
 
{{out}}
Line 4,051 ⟶ 5,750:
</html
</pre>
 
=={{header|Stata}}==
First, a program to write a Stata matrix to an HTML file.
 
<syntaxhighlight lang="stata">program mat2html
local nr = rowsof(`1')
local nc = colsof(`1')
local rn `: rownames `1''
local cn `: colnames `1''
tempname f
qui file open `f' using `2', write text replace
file write `f' "<!doctype html>" _n
file write `f' "<html>" _n
file write `f' "<head>" _n
file write `f' `"<meta charset="UTF-8">"' _n
file write `f' "</head>" _n
file write `f' "<body>" _n
file write `f' `"<table border="1">"' _n
* write column names
file write `f' "<tr>" _n
file write `f' "<td></td>" _n
forv j = 1/`nc' {
local s `: word `j' of `cn''
file write `f' `"<td>`s'</td>"' _n
}
file write `f' "</tr>" _n
* write row names & data
forv i = 1/`nr' {
file write `f' "<tr>" _n
local s `: word `i' of `rn''
file write `f' `"<td>`s'</td>"' _n
forv j = 1/`nc' {
file write `f' `"<td>`=el(`1',`i',`j')'</td>"' _n
}
file write `f' "</tr>" _n
}
file write `f' "</table>" _n
file write `f' "</body>" _n
file write `f' "</html>" _n
file close `f'
end</syntaxhighlight>
 
An example:
 
<syntaxhighlight lang="stata">matrix a=2,9,4\7,5,3\6,1,8
matrix rownames a = A B C
matrix colnames a = X Y Z
mat2html a magic.html</syntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl"># Make ourselves a very simple templating lib; just two commands
proc TAG {name args} {
set body [lindex $args end]
Line 4,093 ⟶ 5,840:
}]
}]
}]</langsyntaxhighlight>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
tablefile="table.html"
Line 4,116 ⟶ 5,864:
ENDACCESS d
BROWSE $tablefile
</syntaxhighlight>
</lang>
{{out}}
<pre style='height:30ex;overflow:scroll'>
Line 4,136 ⟶ 5,884:
=={{header|UNIX Shell}}==
{{works with|ksh93}}
<langsyntaxhighlight lang="bash">function emit_table {
nameref d=$1
typeset -i idx=0
Line 4,173 ⟶ 5,921:
done
 
emit_table data</langsyntaxhighlight>
{{out}}
<pre><table>
Line 4,192 ⟶ 5,940:
=={{header|Ursa}}==
This program outputs the HTML table to the console.
<langsyntaxhighlight lang="ursa">decl ursa.util.random random
 
out "<table>" endl console
Line 4,209 ⟶ 5,957:
end for
 
out "</table>" endl console</langsyntaxhighlight>
 
=={{header|VBA}}==
<syntaxhighlight lang="vba">
<lang VBA>
Public Sub BuildHTMLTable()
'simple HTML table, represented as a string matrix "cells"
Line 4,267 ⟶ 6,015:
HTMLWrap = sOpenTag & s & sClosingTag
End Function
</syntaxhighlight>
</lang>
 
Subroutine BuildHTMLTable builds the HTML code as one big string.
Line 4,280 ⟶ 6,028:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
Line 4,321 ⟶ 6,069:
objOutHTML.Close
Set objFSO = Nothing
</syntaxhighlight>
</lang>
 
{{In}}
Line 4,350 ⟶ 6,098:
<tr align="right"><td>4</td><td>3672</td><td>6667</td><td>6578</td></tr>
</table>
 
=={{header|Visual Basic .NET}}==
VB.NET has XML literals with embedded expressions (<%= ... %>) similar to [[#Scala|Scala]]. This example additionally uses LINQ query syntax.
<syntaxhighlight lang="vbnet">Module Program
Sub Main()
Const ROWS = 3
Const COLS = 3
 
Dim rand As New Random(0)
Dim getNumber = Function() rand.Next(10000)
 
Dim result =
<table cellspacing="4" style="text-align:right; border:1px solid;">
<tr>
<th></th>
<th>X</th>
<th>Y</th>
<th>Z</th>
</tr>
<%= From c In Enumerable.Range(1, COLS) Select
<tr>
<th><%= c %></th>
<%= From r In Enumerable.Range(1, ROWS) Select
<td><%= getNumber() %></td>
%>
</tr>
%>
</table>
 
Console.WriteLine(result)
End Sub
End Module</syntaxhighlight>
 
{{out}}
<table cellspacing="4" style="text-align:right; border:1px solid;">
<tr>
<th></th>
<th>X</th>
<th>Y</th>
<th>Z</th>
</tr>
<tr>
<th>1</th>
<td>7262</td>
<td>8173</td>
<td>7680</td>
</tr>
<tr>
<th>2</th>
<td>5581</td>
<td>2060</td>
<td>5588</td>
</tr>
<tr>
<th>3</th>
<td>9060</td>
<td>4421</td>
<td>9775</td>
</tr>
</table>
 
{{out}}
<pre>
<table cellspacing="4" style="text-align:right; border:1px solid;">
<tr>
<th></th>
<th>X</th>
<th>Y</th>
<th>Z</th>
</tr>
<tr>
<th>1</th>
<td>7262</td>
<td>8173</td>
<td>7680</td>
</tr>
<tr>
<th>2</th>
<td>5581</td>
<td>2060</td>
<td>5588</td>
</tr>
<tr>
<th>3</th>
<td>9060</td>
<td>4421</td>
<td>9775</td>
</tr>
</table>
</pre>
 
===PHP translation===
{{trans|PHP}}
Just for fun, this attempts to emulate the PHP template engine implementation as much as possible.
 
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.
<syntaxhighlight lang="vbnet">Module Program
Sub Main()
Dim rand As Func(Of Integer, Integer) = AddressOf New Random(0).Next
 
Dim cols = {"", "X", "Y", "Z"}
Dim rows = 3
 
Dim result =
<html>
<body>
<table>
<colgroup>
<%= Iterator Function()
For Each col In cols
Yield <col style="text-align: left;" />
Next
End Function() %>
</colgroup>
<thead>
<tr>
<%= Iterator Function()
For Each col In cols
Yield <td><%= col %></td>
Next
End Function() %>
</tr>
</thead>
<tbody>
<%= Iterator Function()
For r = 1 To rows
Yield _
<tr>
<%= Iterator Function()
For key = 0 To cols.Length - 1
Dim col = cols(key)
Yield <td><%= If(key > 0, rand(10000), r) %></td>
Next
End Function() %>
</tr>
Next
End Function() %>
</tbody>
</table>
</body>
</html>
 
Console.WriteLine(result)
End Sub
End Module</syntaxhighlight>
 
{{out}}
<pre>
<html>
<body>
<table>
<colgroup>
<col style="text-align: left;" />
<col style="text-align: left;" />
<col style="text-align: left;" />
<col style="text-align: left;" />
</colgroup>
<thead>
<tr>
<th></th>
<th>X</th>
<th>Y</th>
<th>Z</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>7262</td>
<td>8173</td>
<td>7680</td>
</tr>
<tr>
<td>2</td>
<td>5581</td>
<td>2060</td>
<td>5588</td>
</tr>
<tr>
<td>3</td>
<td>9060</td>
<td>4421</td>
<td>9775</td>
</tr>
</tbody>
</table>
</body>
</html>
</pre>
 
A more idiomatic version that uses LINQ instead of loops:
<syntaxhighlight lang="vbnet">Module Program
Sub Main()
Dim rand As Func(Of Integer, Integer) = AddressOf New Random(0).Next
 
Dim cols = {"", "X", "Y", "Z"}
Dim rows = 3
 
Dim result =
<html>
<body>
<table>
<colgroup>
<%= cols.Select(Function(__) <col style="text-align: left;"/>) %>
</colgroup>
<thead>
<tr>
<%= cols.Select(Function(col) <td><%= col %></td>) %>
</tr>
</thead>
<tbody>
<%= Enumerable.Range(1, rows).Select(
Function(r) _
<tr>
<%= cols.Select(
Function(col, key) <td><%= If(key > 0, rand(10000), r) %></td>)
%>
</tr>)
%>
</tbody>
</table>
</body>
</html>
 
Console.WriteLine(result)
End Sub
End Module</syntaxhighlight>
 
=={{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}}==
 
<langsyntaxhighlight lang="xml"><?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="4.01" indent="yes"/>
Line 4,410 ⟶ 6,453:
</tr>
</xsl:template>
</xsl:stylesheet></langsyntaxhighlight>
 
XSLT does not have a standard PRNG facility, so a list of numbers is given as input.
Line 4,416 ⟶ 6,459:
Sample input:
 
<langsyntaxhighlight lang="xml"><?xml version="1.0" encoding="UTF-8"?>
<numbers>
<number>1578</number>
Line 4,433 ⟶ 6,476:
<number>1403</number>
<number>4637</number>
</numbers></langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="html"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
Line 4,480 ⟶ 6,523:
</tr>
</table></body>
</html></langsyntaxhighlight>
 
{{out}}
Line 4,523 ⟶ 6,566:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">table:=0'|<table style="text-align:center; border: 1px solid">|
"<th></th><th>X</th><th>Y</th><th>Z</th><tr>";
table=Sink(table);
Line 4,531 ⟶ 6,574:
table.write("</tr>");
}
table.write("\n</table>\n").close().print();</langsyntaxhighlight>
{{out}}
<table style="text-align:center; border: 1px solid"><th></th><th>X</th><th>Y</th><th>Z</th><tr>
2,058

edits