How to take care of the Index Error when using STRING_SPLIT?

拜拜、爱过 提交于 2021-02-10 20:52:31

问题


raised GNAT.STRING_SPLIT.INDEX_ERROR

I have try taking care of using the index error with https://marc.info/?l=gcc-patches&m=139022610224587&w=2 . However, How can it read the next line. Ada doesn't seem it has something similar to nextLine(); like in 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

回答1:


Your problem isn’t that you’ve failed to read the next line (Get_Line (File) does that quite nicely).

The problem is with

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;

You should try to explain to yourself why you have a loop there at all.

Which slice is Code in? Which slice is PayRate in? What happens the second time round the loop?



来源:https://stackoverflow.com/questions/64648412/how-to-take-care-of-the-index-error-when-using-string-split

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!