comp.lang.ada
 help / color / mirror / Atom feed
From: brbarkstrom@gmail.com
Subject: Re: array of string
Date: Mon, 6 Oct 2014 18:06:54 -0700 (PDT)
Date: 2014-10-06T18:06:54-07:00	[thread overview]
Message-ID: <570672e5-a8ba-407c-a04a-9bf4cc723708@googlegroups.com> (raw)
In-Reply-To: <3ffbdc6a-e767-4de1-922f-c9c1ec748f4d@googlegroups.com>

On Friday, October 3, 2014 7:29:15 PM UTC-4, 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?

You could also create an array of Bounded_Strings.  When you set up such
an array, you can specify the maximum size.  Then when you assign a particular
array element, you use the Bounded_Strings package functions to assign a
string to the array.  In other words, you can write

Std_Vstring_Length : constant := 256;
package Vstring is new Ada.Strings.Bounded.Generic_Bounded_Length(Std_Vstring_Length);

Then you should be able to define

Max_Array_Size : constant := 10; -- or whatever the number of strings you want
Bounded_String_Array : array (1 .. Max_Array_Size) of Vstring.Bounded_String;

later, you can do things like

Bounded_String_Array(1) := Vstring.To_Bounded_String("london");
Bounded_String_Array(2) := Vstring.To_Bounded_String("toronto");

If you want to print the strings, you can do things like

Text_IO.Put(Vstring.To_String(Bounded_String_Array(1)));

which should output just london

while 

Text_IO.Put_Line(Vstring.To_String(Bounded_String_Array(2)));

would ouput toronto on a single line.

There are a number of useful functions, including Slice and Element
in the Bounded_Strings package.  You can even use Direct_IO to 
save the elements of the array Bounded_String_Array so you can
retrieve them by doing 

package Bounded_String_DIO is new Direct_IO(Bounded_String_Array); use Bounded_String_DIO;

Then you can do things like

Bounded_String_DIO.Write(File => Bounded_String_DIO_File,
                         Item => Bounded_String_Array(4),
                         To   => Bounded_String.Positive_Count(4));

and read it with

Bounded_String_DIO.Read(File => Bounded_String_DIO_File,
                        Item => Selected_Bounded_String,
                        From => Bounded_String.Positive_Count(4));

which will read the Bounded_String_Array element 4 into 

Selected_Bounded_String : Vstring.Bounded_String;

Hope this helps.

Bruce B.


  parent reply	other threads:[~2014-10-07  1:06 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
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 [this message]
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