There is auxiliary tools for this particular format setup.
procedure Put(Item : in Num; Width : in Field := Default_Width; Base : in Number_Base := Default_Base);
Puts a field with Item
aligned to right and white-space character as filler. Where Width
is field width and Base
is 10 as defualt.
function Head (Source : in String; Count : in Natural; Pad : in Character := Space) return String;
function Tail (Source : in String; Count : in Natural; Pad : in Character := Space) return String;
Returns a formatted string. Where Count
is the field width and Pad
is filler for the field. Head
aligns string to left. Tail
aligns string to right.
Let column width be 8 character long and use dash as filler.
Put_Line (Head ("Ashley", 8, '-'));
Put_Line (Head ("Aloha", 8, '-'));
Put_Line (Head ("Jack", 8, '-'));
Put_Line (Tail ("Ashley", 8, '-'));
Put_Line (Tail ("Aloha", 8, '-'));
Put_Line (Tail ("Jack", 8, '-'));
Output
Ashley--
Aloha---
Jack----
--Ashley
---Aloha
----Jack
Attribute discrete_type'Width
Returns a length which the discrete type requires to be represented as text.
Example
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Calendar; use Ada.Calendar;
procedure Test is
subtype Index is Positive range 95 .. 1223;
procedure Put_Line ( I : in out Index; Name : String; Phone : Natural; Address : String; T : in out Time ) is
begin
Put (I, Index'Width);
Put (": ");
Put (Head (Name, 10, ' '));
Put (" | ");
Put (Tail (Phone'Img (Phone'Img'First + 1 .. Phone'Img'Last), 13, '0'));
Put (" | ");
Put (Head (Address, 20, ' '));
Put (Year (T), Year_Number'Width);
Put ("-");
Put (Month (T), Month_Number'Width);
Put ("-");
Put (Day (T), Day_Number'Width);
I := Positive'Succ (I);
T := T + Duration (60 * 60 * 24 * 3);
New_Line;
end;
I : Index := Index'First;
Now : Time := Clock;
begin
Put_Line (I, "Ashley", 1033438392, "Wellington, New Zealand", Now);
Put_Line (I, "Aloha", 01087651234, "Hawaii, United States of America", Now);
Put_Line (I, "Jack", 01082840184, "Beijing, China", Now);
I := Index'Last - 3;
Put_Line (I,"Ashley", 1033438392, "Wellington, New Zealand", Now);
Put_Line (I,"Aloha", 01087651234, "Hawaii, United States of America", Now);
Put_Line (I,"Jack", 01082840184, "Beijing, China", Now);
end;
Output
95: Ashley | 0001033438392 | Wellington, New Zeal 2015- 5- 24
96: Aloha | 0001087651234 | Hawaii, United State 2015- 5- 27
97: Jack | 0001082840184 | Beijing, China 2015- 5- 30
1220: Ashley | 0001033438392 | Wellington, New Zeal 2015- 6- 2
1221: Aloha | 0001087651234 | Hawaii, United State 2015- 6- 5
1222: Jack | 0001082840184 | Beijing, China 2015- 6- 8
I would recommend to create a type for a phone number, I dunno if it should be string or number with the about of heading zeros, phone number can have different length I guess.