如何使用整数数组拆分字符串?

时间:2013-06-02 16:40:32

标签: parsing ada


我试图使用整数数组作为掩码拆分字符串。 任务很简单,但我不习惯ADA(这是一个约束)。

这是我的代码。它的工作原理是我在测试文件时有一个字符偏移量。有人可以帮我删除这个偏移。它让我疯了。

generic_functions.adb:

package body Generic_Functions is

   -- | Sums up the elements of an array of Integers
   function Sum_Arr_Int(Arr_To_Sum: Int_Array) return Integer is
      Sum: Integer;
   begin
      Sum := 0;
      for I in Arr_To_Sum'Range loop
     Sum := Sum + Arr_To_Sum(I);
      end loop;
      return Sum;
   end Sum_Arr_Int;

   -- | Split up a String into a array of Unbounded_String following pattern from an Int_Array
   function Take_Enregistrements(Decoup_Tab: Int_Array; Str_To_Read: String) return Str_Array is
      Previous, Next : Integer;
      Arr_To_Return  : Str_Array(Decoup_Tab'Range);
   begin
      if Sum_Arr_Int(Decoup_Tab) > Str_To_Read'Length then
     raise Constraint_Error;
      else
     Previous := Decoup_Tab'First;
     Next     := Decoup_Tab(Decoup_Tab'First);
     for I in Decoup_Tab'Range loop
        if I /= Decoup_Tab'First then
           Previous          := Next + 1;
           Next              := (Previous - 1) + Decoup_Tab(I);
        end if;
        Arr_To_Return(I)  := To_Unbounded_String(Str_To_Read(Previous..Next));
     end loop;
     return Arr_To_Return;
      end if;
   end Take_Enregistrements;

end Generic_Functions;

generic_functions.ads:

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

package Generic_Functions is

   -- | Types
   type Int_Array is array(Positive range <>) of Integer;
   type Str_Array is array(Positive range <>) of Unbounded_String;
   -- | end of Types

   -- | Functions
   function Sum_Arr_Int(Arr_To_Sum: Int_Array) return Integer;
   function Take_Enregistrements(Decoup_Tab: Int_Array; Str_To_Read: String) return Str_Array;
   -- | end of Functions

end Generic_Functions;

generic_functions_tests.adb:

with Ada.Text_IO;           use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

with Generic_Functions;     use Generic_Functions;

procedure Generic_Functions_Tests is

   -- | Variables
   Decoup_Test   : constant Int_Array(1..8) := (11, 19, 60, 24, 255, 10, 50, 255);
   Test_Str_Arr  : Str_Array(Decoup_Test'Range);
   Test_Str_Arr2 : Str_Array(Decoup_Test'Range);
   Test_Str_Arr3 : Str_Array(Decoup_Test'Range);
   --Test_Int      : Integer;
   Test_Handle   : File_Type;
   -- | end of Variables

begin

   Open(Test_Handle, In_File, "EXPORTFINAL.DAT");
   Test_Str_Arr  := Take_Enregistrements(Decoup_Test, Get_Line(Test_Handle));
   Test_Str_Arr2 := Take_Enregistrements(Decoup_Test, Get_Line(Test_Handle));
   Test_Str_Arr3 := Take_Enregistrements(Decoup_Test, Get_Line(Test_Handle));

   for I in Test_Str_Arr'Range loop
      Put_Line(To_String(Test_Str_Arr(I)));
   end loop;

   for I in Test_Str_Arr2'Range loop
      Put_Line(To_String(Test_Str_Arr2(I)));
   end loop;

   for I in Test_Str_Arr3'Range loop
      Put_Line(To_String(Test_Str_Arr3(I)));
   end loop;

   --  for I in Test_Str_Arr'Range loop
   --     Test_Int := To_String(Test_Str_Arr(I))'Length;
   --     Put_Line(Integer'Image(Test_Int));
   --  end loop;


   --  for I in Test_Str_Arr2'Range loop
   --     Test_Int := To_String(Test_Str_Arr2(I))'Length;
   --     Put_Line(Integer'Image(Test_Int));
   --  end loop;

   --  for I in Test_Str_Arr3'Range loop
   --     Test_Int := To_String(Test_Str_Arr3(I))'Length;
   --     Put_Line(Integer'Image(Test_Int));
   --  end loop;

   Close(Test_Handle);

end Generic_Functions_Tests;

最后文件:

000000000012012-01-01 10:00:00                                                         IBM                     IBM                                                                                                                                                                                                                     COMPAGNIE IBM FRANCE 17 AVENUE DE l'EUROPE     92275                               BOIS-COLOMBES CEDEX                                                                                                                                                                                                                                 CONFIGURATION COMPLETE SERVEUR000000000000000000000019    .6000000000001000000000000000000001000.00000000000000000000000000000196.00000000000000000000000000001196.00000000
000000000022012-01-01 11:00:00                                                   MICROSOFT                     MSC                                                                                                                                                                                                                                 39 QUAI DU PRESIDENT ROOSEVELT     92130                               ISSY-LES-MOULINEAUX                                                                                                                                                                                                                                 AMENAGEMENT SALLE INFORMATIQUE000000000000000000000019.6000000000001000000000000000000001000.00000000000000000000000000000196.00000000000000000000000000001196.00000000
000000000032012-01-01 12:00:00                                                   MICROSOFT                     MSC                                                                                                                                                                                                                                 39 QUAI DU PRESIDENT ROOSEVELT     92130                               ISSY-LES-MOULINEAUX                                                                                                                                                                                                                                                 TESTS SUR SITE000000000000000000000019.6000000000001000000000000000000003226.52000000000000000000000000000632.39792000000000000000000000003858.91792000                                                                                                                                                                                                                                     DELEGATION TECHNICIEN HARD000000000000000000000019.60000000000000000000000000000001.00000000000000000000000000001000.00000000000000000000000000000196.00000000000000000000000000001196.00000000

1 个答案:

答案 0 :(得分:1)

这些行:

if I = Decoup_Tab'Last then
   Arr_To_Return(I)     := To_Unbounded_String(Str_To_Read(Previous..Next));
end if;

将覆盖数组中的最后一个元素。

此外,您确定行号(00000000001,00000000002等)是您要根据整数掩码拆分的字符串之一吗?由于您的代码现在是,您使用“11”两次,一次用于行号,一次用于日期字段。如果你跳过行号,其他数字似乎更有意义。