使用 STRING_SPLIT 时如何处理索引错误?
How to take care of the Index Error when using STRING_SPLIT?
提出GNAT.STRING_SPLIT.INDEX_ERROR
我已尝试使用 https://marc.info/?l=gcc-patches&m=139022610224587&w=2 处理索引错误。但是,它如何读取下一行。 Ada 似乎没有类似于 nextLine() 的东西;就像 java.
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 TextFile is
File : File_Type;
Tokens : Slice_Set;
Index : Slice_Number;
type Payrate_Type is delta 0.01 range 0.00 .. 1000.00;
type Line_Info is record
Code : Unbounded_String := Null_Unbounded_String;
Department : Unbounded_String := Null_Unbounded_String;
Name : Unbounded_String := Null_Unbounded_String;
Title : Unbounded_String := Null_Unbounded_String;
ID : Natural :=0;
Payrate : Payrate_Type:=0.00;
end record;
A_Line : Line_Info;
package Payrate_IO is new Ada.Text_IO.Fixed_IO(Payrate_Type);
Last : Positive;
begin
Open (File, In_File, "Store.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
A_Line.Code := To_Unbounded_String(Slice(Tokens, I));
A_Line.Department := To_Unbounded_String(Slice(Tokens, I+1));
A_Line.Name := To_Unbounded_String(Slice(Tokens, I+2));
A_Line.Title := To_Unbounded_String(Slice(Tokens, I+3));
A_Line.ID := Natural'Value(Slice(Tokens, I+4));
Payrate_IO.Get(Slice(Tokens,I+5),A_Line.Payrate,Last);
end loop;
end loop;
exception
when End_Error =>
if Is_Open(File) then
Close (File);
end if;
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
你的问题不是你没有读到下一行(Get_Line (File)
做得很好)。
问题在于
for I in 1 .. Slice_Count (Tokens) loop
A_Line.Code := To_Unbounded_String(Slice(Tokens, I));
A_Line.Department := To_Unbounded_String(Slice(Tokens, I+1));
A_Line.Name := To_Unbounded_String(Slice(Tokens, I+2));
A_Line.Title := To_Unbounded_String(Slice(Tokens, I+3));
A_Line.ID := Natural'Value(Slice(Tokens, I+4));
Payrate_IO.Get(Slice(Tokens,I+5),A_Line.Payrate,Last);
end loop;
你应该试着向自己解释为什么你在那里有一个循环。
Code
属于哪个切片? PayRate
在哪个切片中?第二次循环会发生什么?
提出GNAT.STRING_SPLIT.INDEX_ERROR
我已尝试使用 https://marc.info/?l=gcc-patches&m=139022610224587&w=2 处理索引错误。但是,它如何读取下一行。 Ada 似乎没有类似于 nextLine() 的东西;就像 java.
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 TextFile is
File : File_Type;
Tokens : Slice_Set;
Index : Slice_Number;
type Payrate_Type is delta 0.01 range 0.00 .. 1000.00;
type Line_Info is record
Code : Unbounded_String := Null_Unbounded_String;
Department : Unbounded_String := Null_Unbounded_String;
Name : Unbounded_String := Null_Unbounded_String;
Title : Unbounded_String := Null_Unbounded_String;
ID : Natural :=0;
Payrate : Payrate_Type:=0.00;
end record;
A_Line : Line_Info;
package Payrate_IO is new Ada.Text_IO.Fixed_IO(Payrate_Type);
Last : Positive;
begin
Open (File, In_File, "Store.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
A_Line.Code := To_Unbounded_String(Slice(Tokens, I));
A_Line.Department := To_Unbounded_String(Slice(Tokens, I+1));
A_Line.Name := To_Unbounded_String(Slice(Tokens, I+2));
A_Line.Title := To_Unbounded_String(Slice(Tokens, I+3));
A_Line.ID := Natural'Value(Slice(Tokens, I+4));
Payrate_IO.Get(Slice(Tokens,I+5),A_Line.Payrate,Last);
end loop;
end loop;
exception
when End_Error =>
if Is_Open(File) then
Close (File);
end if;
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
你的问题不是你没有读到下一行(Get_Line (File)
做得很好)。
问题在于
for I in 1 .. Slice_Count (Tokens) loop
A_Line.Code := To_Unbounded_String(Slice(Tokens, I));
A_Line.Department := To_Unbounded_String(Slice(Tokens, I+1));
A_Line.Name := To_Unbounded_String(Slice(Tokens, I+2));
A_Line.Title := To_Unbounded_String(Slice(Tokens, I+3));
A_Line.ID := Natural'Value(Slice(Tokens, I+4));
Payrate_IO.Get(Slice(Tokens,I+5),A_Line.Payrate,Last);
end loop;
你应该试着向自己解释为什么你在那里有一个循环。
Code
属于哪个切片? PayRate
在哪个切片中?第二次循环会发生什么?