如何将字符串的不同部分解析为单独的变量?
How to parse different parts of a string into separate variables?
我如何获取“IL”然后将其保存到 TempCode 中,这是一个字符串等等,为每个单词、整数和浮点数提供一个临时变量。然后获取下一个TempCode等等。重点是在代码列下获取某个代码,然后执行该操作并获取要使用的部门、Name/Vendor、职位、ID 和工资率。
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
首先,您要为您的支付率定义一个类型。浮点数可以,但我建议改用定点类型,因为它可以根据您的需要打印更清晰的内容。
type Payrate_Type is delta 0.01 range 0.00 .. 1000.00;
要读入您的类型的值,您需要实例化泛型 Ada.Text_IO.Fixed_IO:
package Payrate_IO is new Ada.Text_IO.Fixed_IO(Payrate_Type);
接下来我会将每个字段的所有变量分组到一条记录中。使用 Unbounded_String 存储字符串,Natural 用于 ID,您的支付率类型用于支付率。
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;
然后对于 while 循环的每次迭代,而不是 for 循环,您只需为每个不同的切片片段单独赋值:
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);
当您的输入不正确时,您需要执行一些异常处理逻辑来弥补。我会留给你解决。
这是您的输入集的测试程序:
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;
并且输出:
$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
请注意,我取出了您的文件类型和调用,这样我就可以使用标准输入作为输入源进行非常快速的测试。
我如何获取“IL”然后将其保存到 TempCode 中,这是一个字符串等等,为每个单词、整数和浮点数提供一个临时变量。然后获取下一个TempCode等等。重点是在代码列下获取某个代码,然后执行该操作并获取要使用的部门、Name/Vendor、职位、ID 和工资率。
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
首先,您要为您的支付率定义一个类型。浮点数可以,但我建议改用定点类型,因为它可以根据您的需要打印更清晰的内容。
type Payrate_Type is delta 0.01 range 0.00 .. 1000.00;
要读入您的类型的值,您需要实例化泛型 Ada.Text_IO.Fixed_IO:
package Payrate_IO is new Ada.Text_IO.Fixed_IO(Payrate_Type);
接下来我会将每个字段的所有变量分组到一条记录中。使用 Unbounded_String 存储字符串,Natural 用于 ID,您的支付率类型用于支付率。
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;
然后对于 while 循环的每次迭代,而不是 for 循环,您只需为每个不同的切片片段单独赋值:
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);
当您的输入不正确时,您需要执行一些异常处理逻辑来弥补。我会留给你解决。
这是您的输入集的测试程序:
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;
并且输出:
$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
请注意,我取出了您的文件类型和调用,这样我就可以使用标准输入作为输入源进行非常快速的测试。