Create an HTML table: Difference between revisions

m (→‎{{header|REXX}}: added a comment in the header section. -- ~~~~)
Line 9:
=={{header|Ada}}==
 
We define a generic package to output HTML tables:
<lang Ada>with Ada.Text_IO; with Ada.Numerics.Discrete_Random;
 
<lang Ada>generic
procedure HTML_Table is
type Item_Type is private;
with function To_String(Item: Item_Type) return String is <>;
with procedure Put(S: String) is <>;
with procedure Put_Line(Line: String) is <>;
package HTML_Table is
 
type Callback is not null access function (Number: Positive) return String;
use Ada.Text_IO;
 
type Item_Array is array(Positive range <>, Positive range <>) of Item_Type;
function Blanks(N: Natural) return String is
begin
if N=0 then
return "";
else
return " " & Blanks(N-1);
end if;
end Blanks;
 
procedure Print(Items: Item_Array; Get_Header: Callback);
package Table is
type Four_Digits is mod 10_000;
package Rand is new Ada.Numerics.Discrete_Random(Four_Digits);
Gen: Rand.Generator;
 
end HTML_Table;</lang>
type object is tagged null record;
 
The implementation goes as follows:
procedure Print(T: Object; Rows: Natural);
procedure Print_Header(T: Object);
procedure Print_Body(T: Object; Rows: Natural);
procedure Print_Row(T: Object; Row_Number: Positive);
end Table;
 
<lang Ada>package body TableHTML_Table is
 
procedure Print_RowPrint(TItems: ObjectItem_Array; Row_NumberGet_Header: PositiveCallback) is
begin
Put(Blanks(4) & "<tr><td>" & Positive'Image(Row_Number) & "</td>");
for I in 1 .. 3 loop
Put("<td>" & Four_Digits'Image(Rand.Random(Gen)) & "</td>");
end loop;
Put_Line("</tr>");
end Print_Row;
 
function procedure Print_BodyBlanks(TN: Object;Natural) Rows:return Natural)String is
-- indention for better readable HTML
begin
begin
Put_Line(Blanks(2)&"<tbody align = ""right"">");
if forN=0 I in 1 .. Rows loopthen
return T.Print_Row(I)"";
end loop;else
Put_Line( return " " & Blanks(2)&"</tbody>"N-1);
end Print_Bodyif;
end Blanks;
 
procedure Print_HeaderPrint_Row(TRow_Number: ObjectPositive) is
begin
Put_LinePut(Blanks(24) & "<theadtr><td>" align& =Positive'Image(Row_Number) & ""right""</td>");
for I in Items'Range(2) loop
Put_Line(Blanks(4) & "<tr><th></th><td>X</td><td>Y</td>" &
Put("<td>" & To_String(Items(Row_Number, I)) & "<td>Z</td></tr>");
Put_Line(Blanks(2) & "</thead>") end loop;
end Print_Header Put_Line("</tr>");
end Print_Row;
 
procedure Print(T: Object; Rows: Natural)Print_Body is
begin
Put_Line(Blanks(2)&"<tabletbody align = ""right"">");
for T.Print_Header;I in Items'Range(1) loop
T.Print_Body Print_Row(RowsI);
end Put_Line("</table>")loop;
end Print Put_Line(Blanks(2)&"</tbody>");
end Print_Body;
 
procedure Print_Header is
begin
Rand.Reset(Gen);begin
Put_Line(Blanks(2) & "<thead align = ""right"">");
end Table;
Put(Blanks(4) & "<tr><th></th>");
for I in Items'Range(2) loop
Put("<td>" & Get_Header(I) & "</td>");
end loop;
Put_Line("</tr>");
Put_Line(Blanks(2) & "</thead>");
end Print_Header;
 
begin
T: Table.Object;
Put_Line("<table>");
Print_Header;
Print_Body;
Put_Line("</table>");
end Print;
 
begin
T.Print(Rows => 4);
end HTML_Table;</lang>
 
Finally, the implementation using an instance of HTML_Table:
 
<lang Ada>with Ada.Text_IO, Ada.Numerics.Discrete_Random, HTML_Table;
 
procedure Test_HTML_Table is
 
-- define the Item_Type and the random generator
type Four_Digits is mod 10_000;
package Rand is new Ada.Numerics.Discrete_Random(Four_Digits);
Gen: Rand.Generator;
 
-- now we instantiate the generic package HTML_Table
package Table is new HTML_Table
(Item_Type => Four_Digits,
To_String => Four_Digits'Image,
Put => Ada.Text_IO.Put,
Put_Line => Ada.Text_IO.Put_Line);
 
-- define the object that will the values that the table contains
The_Table: Table.Item_Array(1 .. 4, 1..3);
 
-- this function outputs the column heads
function Head(P: Positive) return String is
begin
case P is
when 1 => return "X";
when 2 => return "Y";
when 3 => return "Z";
when others => raise Program_Error;
end case;
end Head;
 
begin
-- fill The_Table with random values
Rand.Reset(Gen);
for Rows in The_Table'Range(1) loop
for Cols in The_Table'Range(2) loop
The_Table(Rows, Cols) := Rand.Random(Gen);
end loop;
end loop;
 
-- output The_Table
Table.Print(The_Table, Head'Access);
end Test_HTML_Table;</lang>
 
Each time you run the program, you get different random values for the table. Here is a sample output:
Line 90 ⟶ 132:
</thead>
<tbody align = "right">
<tr><td> 1</td><td> 50917255</td><td> 3953014</td><td> 63199436</td></tr>
<tr><td> 2</td><td> 7099554</td><td> 7463314</td><td> 1738765</td></tr>
<tr><td> 3</td><td> 17014832</td><td> 3897129</td><td> 21242048</td></tr>
<tr><td> 4</td><td> 853931</td><td> 16966897</td><td> 14178265</td></tr>
</tbody>
</table></lang>
lucks@medsec</lang>
 
Viewing the output with Lynx:
 
<pre> X Y Z
1 7255 3014 9436
1 5091 395 6319
2 554 3314 8765
2 7099 746 173
3 4832 129 2048
3 1701 3897 2124
4 8539 1696 141731 6897 8265
</pre>
 
=={{header|AutoHotkey}}==
{{trans|C}}
Anonymous user