Reputation: 65
How do I get "IL" then saving it to TempCode which is a string and so on to giving each word, integer and float a Temporary variable. Then get the next TempCode and so on. The whole point is to get a certain code under Code Column then do that operation and get the Department, Name/Vendor,Title,ID and Payrate to be use.
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.String_Split; use GNAT.String_Split;
procedure TextFile is
File : File_Type;
Tokens : Slice_Set;
--Index : Slice_Number;
TempCode: String := "";
begin
Open (File, In_File, "DynList.txt");
-- Skip the file header
Skip_Line (File);
-- Read the data
while not End_Of_File (File) loop
-- Split the line from the file on array which contains separated
-- words. Treat multiple spaces as a single separator (don't
-- create empty elements).
Create (Tokens, Get_Line (File), " ", Multiple);
-- Print each of the array's values
for I in 1 .. Slice_Count (Tokens) loop
--I have try using function Separators
Put_Line (Slice (Tokens, I));
end loop;
end loop;
Close (File);
end TextFile;
Store.txt
Code Department Name/Vendor Title ID Payrate
IL Sales John Sales_person 1378 25.46
IR Crew Jesse Sales_person 1379 25.46
Upvotes: 0
Views: 486
Reputation: 3641
First you want to define a type for your payrate. A float will work, but I would recommend making a fixed point type instead as it prints cleaner for what you want.
type Payrate_Type is delta 0.01 range 0.00 .. 1000.00;
To read in values for your type, you will need to instantiate the generic Ada.Text_IO.Fixed_IO:
package Payrate_IO is new Ada.Text_IO.Fixed_IO(Payrate_Type);
Next I would group all your variables for each field in a single record. Use Unbounded_String to store the strings, Natural for the ID, and your pay rate type for your pay rate.
type Line_Info is record
Code : Unbounded_String;
Department : Unbounded_String;
Name : Unbounded_String;
Title : Unbounded_String;
ID : Natural;
Payrate : Payrate_Type;
end record;
A_Line : Line_Info;
Then for each iteration of your while loop, instead of the for loop, you just do individual assignments for each of the various slice pieces:
A_Line.Code := To_Unbounded_String(Slice(Tokens, 1));
A_Line.Department := To_Unbounded_String(Slice(Tokens, 2));
A_Line.Name := To_Unbounded_String(Slice(Tokens, 3));
A_Line.Title := To_Unbounded_String(Slice(Tokens, 4));
A_Line.ID := Natural'Value(Slice(Tokens, 5));
Payrate_IO.Get(Slice(Tokens,6),A_Line.Payrate,Last);
You'll need to do some exception handling logic to cover when your input is not correct. I'll leave that up to you to figure out.
Here's a test program for your input set:
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.String_Split; use GNAT.String_Split;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
procedure Hello is
Tokens : Slice_Set;
--Index : Slice_Number;
TempCode: String := "";
type Payrate_Type is delta 0.01 range 0.00 .. 1000.00;
type Line_Info is record
Code : Unbounded_String;
Department : Unbounded_String;
Name : Unbounded_String;
Title : Unbounded_String;
ID : Natural;
Payrate : Payrate_Type;
end record;
A_Line : Line_Info;
package Payrate_IO is new Ada.Text_IO.Fixed_IO(Payrate_Type);
Last : Positive;
begin
Put_Line("Hello, world!");
Skip_Line;
-- Read the data
while not End_Of_File loop
-- Split the line from the file on array which contains separated
-- words. Treat multiple spaces as a single separator (don't
-- create empty elements).
Create (Tokens, Get_Line, " ", Multiple);
-- Print each of the array's values
A_Line.Code := To_Unbounded_String(Slice(Tokens, 1));
A_Line.Department := To_Unbounded_String(Slice(Tokens, 2));
A_Line.Name := To_Unbounded_String(Slice(Tokens, 3));
A_Line.Title := To_Unbounded_String(Slice(Tokens, 4));
A_Line.ID := Natural'Value(Slice(Tokens, 5));
Payrate_IO.Get(Slice(Tokens,6),A_Line.Payrate,Last);
Put_Line(To_String(A_Line.Code));
Put_Line(To_String(A_Line.Department));
Put_Line(To_String(A_Line.Name));
Put_Line(To_String(A_Line.Title));
Put_Line(A_Line.ID'Image);
Put_Line(A_Line.Payrate'Image);
end loop;
end Hello;
And the output:
$gnatmake -o hello *.adb
gcc -c hello.adb
gnatbind -x hello.ali
gnatlink hello.ali -o hello
$hello
Hello, world!
IL
Sales
John
Sales_person
1378
25.46
IR
Crew
Jesse
Sales_person
1379
25.46
Note that I took out your File type and calls so I could test really quick using standard in as the input source.
Upvotes: 4