使用 STRING_SPLIT 时如何处理索引错误? [英] How to take care of the Index Error when using STRING_SPLIT?

查看:22
本文介绍了使用 STRING_SPLIT 时如何处理索引错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引发 GNAT.STRING_SPLIT.INDEX_ERROR

raised GNAT.STRING_SPLIT.INDEX_ERROR

我尝试使用 https 处理索引错误://marc.info/?l=gcc-patches&m=139022610224587&w=2.但是,它如何读取下一行.Ada 似乎没有类似于 nextLine();就像在 Java 中一样.

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;

商店.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) 做得很好).

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

问题在于

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.

Code 在哪个切片?PayRate 在哪个切片中?第二次循环会发生什么?

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

这篇关于使用 STRING_SPLIT 时如何处理索引错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆