From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,9fa5063fbdc75ae4 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-14 19:55:26 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!small1.nntp.aus1.giganews.com!border3.nntp.aus1.giganews.com!nntp.giganews.com!wn14feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi_feed4!attbi.com!rwcrnsc52.ops.asp.att.net.POSTED!not-for-mail Message-ID: <3F8CB712.2070808@comcast.net> From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: array of strings in a function References: Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 8bit NNTP-Posting-Host: 24.34.139.183 X-Complaints-To: abuse@comcast.net X-Trace: rwcrnsc52.ops.asp.att.net 1066186521 24.34.139.183 (Wed, 15 Oct 2003 02:55:21 GMT) NNTP-Posting-Date: Wed, 15 Oct 2003 02:55:21 GMT Organization: Comcast Online Date: Wed, 15 Oct 2003 02:55:25 GMT Xref: archiver1.google.com comp.lang.ada:869 Date: 2003-10-15T02:55:25+00:00 List-Id: Antonio Mart�nez wrote: > I'm trying to pass to a function an argument. The argument is an > array range <> of Strings (unbounded array). > number of string literals is unknown by the function. And the > length of every string is unknown as well. > How do I work with unbound strings? You can create an array of Unbounded_String (found in Ada.Strings.Unbounded). Or you can use the package below. Note that in either case, you are going to have to create an array object, put the strings in it, and then pass that to the array. With the package below your example above: > my_fun("Hello", ", I'm", " the function"); Becomes: with Ada_Strings_Bounded_Array; use Ada_Strings_Bounded_Array; ... Foo: Bounded_Array(3,13); -- you could choose 20 instead of 13 or any -- number you felt like > 13. begin Set(1, "Hello"); Set(2, ", I'm"); Set(3, " the function"); Bar := my_fun(Foo); -- you didn't say what my_fun returned. ... alternatively: with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; ... type My_Array is array (Integer range <>) of Unbounded_String; Foo: My_Array(1..3); begin My_Array(1) := To_Unbounded_String("Hello"); My_Array(2) := To_Unbounded_String(", I'm"); My_Array(3) := To_Unbounded_String(" the function"); Bar := my_fun(Foo); ... This is not a normal Ada idiom, but it can be done. The normal Ada idiom would be: Bar := my_fun("Hello" & ", I'm" & " the function"); That way you can declare my_fun to take a single string parameter, and use catenation (&) to put the pieces together. The pieces you are putting together can be string literals, character literals, String and Character variables or constants, and functions returning Strings. In fact, you will often see: Ada.Text_IO.Put_Line(" X is " & Integer'Image(X) & '.' ); or something similar. ------------------------------------------------------------------------- package Ada_Strings_Bounded_Array is type Bounded_Array(Size, Max: Natural) is private; procedure Set(B: in out Bounded_Array; Row: in Positive; To: in String); procedure Append(B: in out Bounded_Array; Row: in Positive; Add: in String); function Get(From: Bounded_Array; Row: Positive) return String; function Get_Length(From: Bounded_Array; Row: Positive) return Natural; private type Element_Array is array (Natural range <>, Natural range <>) of Character; type Length_Array is array(Natural range <>) of Natural; type Bounded_Array(Size, Max: Natural) is record Lengths: Length_Array(1..Size) := (others => 0); Contents: Element_Array(1..Size, 1..Max); end record; end Ada_Strings_Bounded_Array; ---------------------------------------------------------------------- package body Ada_Strings_Bounded_Array is procedure Set(B: in out Bounded_Array; Row: in Positive; To: in String) is Temp: String(1..To'Length) := To; -- insure lower bound is 1. begin if To'Length > B.Max then raise Constraint_Error; end if; B.Lengths(Row) := Temp'Last; -- will raise C_E if Row out of bounds. for I in Temp'Range loop B.Contents(Row,I) := Temp(I); end loop; end Set; procedure Append(B: in out Bounded_Array; Row: in Positive; Add: in String) is Temp: String(B.Lengths(Row)+ 1..B.Lengths(Row)+Add'Length) := Add; begin if Temp'Last > B.Max then raise Constraint_Error; end if; B.Lengths(Row) := Temp'Last; for I in Temp'Range loop B.Contents(Row,I) := Temp(I); end loop; end Append; function Get(From: Bounded_Array; Row: Positive) return String is Temp: String(1..From.Lengths(Row)); begin for I in Temp'Range loop Temp(I) := From.Contents(Row,I); end loop; return Temp; end Get; function Get_Length(From: Bounded_Array; Row: Positive) return Natural is begin return From.Lengths(Row); end Get_Length; pragma Inline(Set, Append, Get, Get_Length); end Ada_Strings_Bounded_Array; ------------------------------------------------------------------------- with Ada.Text_IO; use Ada.Text_IO; with Ada_Strings_Bounded_Array; use Ada_Strings_Bounded_Array; procedure Bounded_Array_Test is BA1: Bounded_Array(5, 30); BA2: Bounded_Array(2, 10); begin Set(BA1,1, "Robert I. Eachus"); Set(BA1,2, "Mortimer Q. Snerd"); Set(BA2,1, "foo"); Append(BA2,1, "bar"); Put_Line(Get(BA1,1)); Put_Line(Get(BA1,2)); Put_Line(Get(BA2,1)); if Get_Length(BA1,1) /= 16 then Put_Line("Oops! BA1,1"); end if; if Get_Length(BA2,1) /= 6 then Put_Line("Oops! BA2,1"); end if; if Get_Length(BA1,3) /= 0 then Put_Line("Oops! BA1,3"); end if; end Bounded_Array_Test; -- Robert I. Eachus "Quality is the Buddha. Quality is scientific reality. Quality is the goal of Art. It remains to work these concepts into a practical, down-to-earth context, and for this there is nothing more practical or down-to-earth than what I have been talking about all along...the repair of an old motorcycle." -- from Zen and the Art of Motorcycle Maintenance by Robert Pirsig