2013-06-02 66 views
1


我想分割一個字符串使用整數數組作爲掩碼。 這個任務很簡單,但我不習慣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; 

和finaly文件:

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

這些行:

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

將陣列中覆蓋的最後一個元素。

另外,你確定行號(00000000001,00000000002等)是你想要基於整數掩碼拆分的字符串之一嗎?由於您的代碼是現在,您可以使用'11'兩次,一次用於行號,一次用於日期字段。如果你跳過行號,其他數字似乎更有意義。

+0

你的指針很好。我更新了工作代碼。當我分裂日期時,面具是錯誤的。循環也搞砸了。謝謝。 – Kati