Anonymous user
Create an HTML table: Difference between revisions
→{{header|Ada}}
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>generic
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;
type Item_Array is array(Positive range <>, Positive range <>) of Item_Type;
procedure Print(Items: Item_Array; Get_Header: Callback);
end HTML_Table;</lang>
The implementation goes as follows:
<lang
function
-- indention for better readable HTML
begin
if
return
end
end Blanks;
for I in Items'Range(2) loop
Put("<td>" & To_String(Items(Row_Number, I))
end Print_Row;
for
end
end Print_Body;
procedure Print_Header is
Put_Line(Blanks(2) & "<thead align = ""right"">");
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
Put_Line("<table>");
Print_Header;
Print_Body;
Put_Line("</table>");
end Print;
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>
<tr><td> 2</td><td>
<tr><td> 3</td><td>
<tr><td> 4</td><td>
</tbody>
</table
lucks@medsec</lang>
Viewing the output with Lynx:
<pre> X Y Z
1 7255 3014 9436
2 554 3314 8765
3 4832 129 2048
4
</pre>
=={{header|AutoHotkey}}==
{{trans|C}}
|