comp.lang.ada
 help / color / mirror / Atom feed
From: Brad Moore <brad.moore@shaw.ca>
Subject: Re: array of string
Date: Fri, 03 Oct 2014 19:07:21 -0600
Date: 2014-10-03T19:07:21-06:00	[thread overview]
Message-ID: <cLHXv.346223$_k.274144@fx16.iad> (raw)
In-Reply-To: <a192026a-1509-4e92-81b6-c021cb615a95@googlegroups.com>

On 2014-10-03 6:50 PM, Jerry wrote:
> On Friday, October 3, 2014 4:29:15 PM UTC-7, Stribor40 wrote:
>> is there way to declare array of strings to contain something like this..
>>
>> a(1)="london"
>>
>> a(2)""toronto"
>>
>> how would i create this?
>
> The two examples you give, london and toronto, are of different lengths. The elememts of an array have to be the same (or compatible *) type(s), so you would have to make the strings the same length and then waste some of the length for shorter names, and hope that you have declared the string length long enough for your longest city name.
>
> A better way is to make an array of unbounded strings whereby Ada things each element of the array is the same (an unbounded string) but yet unbounded strings can be of any length. The only thing you'll have to watch out for is that it is likely that other places will expect to see string, not unbounded string, for example, Put. This is easy as Ada provides conversion functions between strings and unbounded strings.
>
> When I was first learning Ada (still am, really), learning about the three different kinds of strings was one of the most useful things to know. Another was to understand subtypes *.
>
> Jerry
>

Some other options include;

1. Create an array type of access to string
eg.

type String_Array is array (Natural range <>) of access constant String;
S1 : aliased constant String := "Toronto";
S2 : aliased constant String := "London";

S : String_Array := (1 => S1'Access, 2 => S2'Access);


2. You could use an indefinite container, such as an indefinite vector.

eg.

package S_Vectors is new Ada.Containers.Indefinite_Vectors
     (Index_Type   => Natural,
      Element_Type => String);

S_Vector : S_Vectors.Vector;

...
S_Vector.Append ("Toronto");
S_Vector.Append ("London");




  reply	other threads:[~2014-10-04  1:07 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-03 23:29 array of string Stribor40
2014-10-04  0:50 ` Jerry
2014-10-04  1:07   ` Brad Moore [this message]
2014-10-04  1:19   ` Stribor40
2014-10-04  1:29     ` Stribor40
2014-10-04  2:01       ` Stribor40
2014-10-04  7:14       ` Simon Wright
2014-10-04  8:32       ` Dmitry A. Kazakov
2014-10-04 14:15         ` AdaMagica
2014-10-04 15:20           ` Dmitry A. Kazakov
2014-10-06 16:58             ` Adam Beneschan
2014-10-04  3:09     ` Shark8
2014-10-04  5:33     ` Brad Moore
2014-10-07  1:06 ` brbarkstrom
2014-10-07 16:06   ` Use of bounded strings? (Was: array of string) Jacob Sparre Andersen
2014-10-07  2:08 ` array of string Jerry
2014-10-07 16:49 ` brbarkstrom
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox