comp.lang.ada
 help / color / mirror / Atom feed
* array of string
@ 2014-10-03 23:29 Stribor40
  2014-10-04  0:50 ` Jerry
                   ` (3 more replies)
  0 siblings, 4 replies; 17+ messages in thread
From: Stribor40 @ 2014-10-03 23:29 UTC (permalink / raw)


is there way to declare array of strings to contain something like this..

a(1)="london"
a(2)""toronto"

how would i create this?

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  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-07  1:06 ` brbarkstrom
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 17+ messages in thread
From: Jerry @ 2014-10-04  0:50 UTC (permalink / raw)


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


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  2014-10-04  0:50 ` Jerry
@ 2014-10-04  1:07   ` Brad Moore
  2014-10-04  1:19   ` Stribor40
  1 sibling, 0 replies; 17+ messages in thread
From: Brad Moore @ 2014-10-04  1:07 UTC (permalink / raw)


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");




^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  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
                       ` (2 more replies)
  1 sibling, 3 replies; 17+ messages in thread
From: Stribor40 @ 2014-10-04  1:19 UTC (permalink / raw)


ok if each string is 3 character long....say first string "abc" and "bcd"..
Example Brad show below is way too complicated for my level

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  2014-10-04  1:19   ` Stribor40
@ 2014-10-04  1:29     ` Stribor40
  2014-10-04  2:01       ` Stribor40
                         ` (2 more replies)
  2014-10-04  3:09     ` Shark8
  2014-10-04  5:33     ` Brad Moore
  2 siblings, 3 replies; 17+ messages in thread
From: Stribor40 @ 2014-10-04  1:29 UTC (permalink / raw)


I tried this...

 type Array_Type is array (1 .. 6, 1 .. 3) of Character;
 myArray : Array_Type; 

 myArray(1,1..3) := "abc"

my compiler was mad at me because of this.....few subscripts in array reference




^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  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
  2 siblings, 0 replies; 17+ messages in thread
From: Stribor40 @ 2014-10-04  2:01 UTC (permalink / raw)


On Friday, 3 October 2014 21:29:19 UTC-4, Stribor40  wrote:
> I tried this...
> 
> 
> 
>  type Array_Type is array (1 .. 6, 1 .. 3) of Character;
> 
>  myArray : Array_Type; 
> 
> 
> 
>  myArray(1,1..3) := "abc"
> 
> 
> 
> my compiler was mad at me because of this.....few subscripts in array reference

making this call put(myArray(1,1)) i was able to get "a" out but how do i get "abc" out to the screen?

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  2014-10-04  1:19   ` Stribor40
  2014-10-04  1:29     ` Stribor40
@ 2014-10-04  3:09     ` Shark8
  2014-10-04  5:33     ` Brad Moore
  2 siblings, 0 replies; 17+ messages in thread
From: Shark8 @ 2014-10-04  3:09 UTC (permalink / raw)


On 10/3/2014 6:19 PM, Stribor40 wrote:
> ok if each string is 3 character long....say first string "abc" and "bcd"..
> Example Brad show below is way too complicated for my level
>

Ah, here's where you /can/ use subtypes:

-- Constrain String to length of 3.
subtype String_3 is String(1..3);

-- Create an unconstrained array of the constrained strings.
Type String_List is Array(Positive range <>) of String_3;

-- Type out the list out want.
-- Note #1: for an array of length 1, you must use named notation.
--          EG: (1 => "xyz")
-- Note #2: for an array of length 0, you must use the null range.
--          EG: ( 2..1 => "xxx" )
Data : constant String_List:= ("abc","bcd");


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  2014-10-04  1:19   ` Stribor40
  2014-10-04  1:29     ` Stribor40
  2014-10-04  3:09     ` Shark8
@ 2014-10-04  5:33     ` Brad Moore
  2 siblings, 0 replies; 17+ messages in thread
From: Brad Moore @ 2014-10-04  5:33 UTC (permalink / raw)


On 2014-10-03 7:19 PM, Stribor40 wrote:
> ok if each string is 3 character long....say first string "abc" and "bcd"..
> Example Brad show below is way too complicated for my level
>

Ok, if you still might be interested in an array of variable length 
strings, here is a simpler example.

type String_Access is access String;

City_Names : array (1 .. 4) of String_Access
         := (new String'("Toronto"),
             new String'("London"),
             new String'("Paris"),
             new String'("Tallahassee");


for I in City_Names'Range loop
    Put_Line(City_Names (I).all);
end loop;



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  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
  2 siblings, 0 replies; 17+ messages in thread
From: Simon Wright @ 2014-10-04  7:14 UTC (permalink / raw)


Stribor40 <ikamzic@gmail.com> writes:

> I tried this...
>
>  type Array_Type is array (1 .. 6, 1 .. 3) of Character;
>  myArray : Array_Type; 
>
>  myArray(1,1..3) := "abc"
>
> my compiler was mad at me because of this.....few subscripts in array
> reference

What about

   with Ada.Text_IO; use Ada.Text_IO;
   procedure Strib is
      type Strings is array (1 .. 6) of String (1 .. 3);
      S : Strings;
   begin
      S (1) := "abc";
      S (2) := "def";
      S (3 .. 6) := ("ghi", "jkl", "mno", "pqr");
      for J in S'Range loop
         Put_Line (S (J));
      end loop;
   end Strib;


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  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
  2 siblings, 1 reply; 17+ messages in thread
From: Dmitry A. Kazakov @ 2014-10-04  8:32 UTC (permalink / raw)


On Fri, 3 Oct 2014 18:29:16 -0700 (PDT), Stribor40 wrote:

> I tried this...
> 
>  type Array_Type is array (1 .. 6, 1 .. 3) of Character;
>  myArray : Array_Type; 
> 
>  myArray(1,1..3) := "abc"
>
> my compiler was mad at me because of this.....few subscripts in array reference

nD arrays don't have slices in Ada.

A language design bug, but nD slices would require a lot of type and
subtype mechanics which is just missing. Note that unlike to 1D slices the
result of a nD slice may be of a different type as in your case. There is a
geometric number of types and their subtypes involved. For 1D slices it is
just another subtype of the same type.

Another language design bug is that you could not define the assignment for
this case either. Assignment is not an operation in Ada.

But you could write a subprogram:

   procedure Set_Row
      (A : in out Array_Type; Row : Positive; Value : String) is
   begin
      if Value'Length /= A'Length (2) then
         raise Constraint_Error with "Subscript error";
      end if;
      for I in Value'Range loop
         A (Row, I - Value'First + A'First (2)) := Value (I);
      end loop;
   end Set_Row;

Then

   Set_Row (myArray, 1, "abc");

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  2014-10-04  8:32       ` Dmitry A. Kazakov
@ 2014-10-04 14:15         ` AdaMagica
  2014-10-04 15:20           ` Dmitry A. Kazakov
  0 siblings, 1 reply; 17+ messages in thread
From: AdaMagica @ 2014-10-04 14:15 UTC (permalink / raw)


On Saturday, October 4, 2014 10:32:44 AM UTC+2, Dmitry A. Kazakov wrote:
> this case either. Assignment is not an operation in Ada.

Just being picky on nomenclature: There is an *assignment statement* in Ada, a part of that is the *assignment operation*.

What you mean, is: Assignment is not an *operator* in Ada.


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  2014-10-04 14:15         ` AdaMagica
@ 2014-10-04 15:20           ` Dmitry A. Kazakov
  2014-10-06 16:58             ` Adam Beneschan
  0 siblings, 1 reply; 17+ messages in thread
From: Dmitry A. Kazakov @ 2014-10-04 15:20 UTC (permalink / raw)


On Sat, 4 Oct 2014 07:15:43 -0700 (PDT), AdaMagica wrote:

> On Saturday, October 4, 2014 10:32:44 AM UTC+2, Dmitry A. Kazakov wrote:
>> this case either. Assignment is not an operation in Ada.
> 
> Just being picky on nomenclature: There is an *assignment statement* in
> Ada, a part of that is the *assignment operation*.
> 
> What you mean, is: Assignment is not an *operator* in Ada.

Not quite.

Though, I prefer "operator" to mean a lexical element of an expression
(e.g. +) and "operation" to mean a subprogram taking the type as an
argument or result (e.g. a primitive operation).

So, yes assignment is not an operator, but I don't want it to be one. I
don't want assignments to appear in expressions, as I didn't want
if-then-else in expressions introduced recently. Both are abomination to
me.

But I want the part of assignment statement you call operation to be a
proper one, primitive for all involved types and overriddable. Randy used
to say that this were impossible due to discriminants, that is IMO untrue.
The real trouble with assignment is multiple dispatch.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  2014-10-04 15:20           ` Dmitry A. Kazakov
@ 2014-10-06 16:58             ` Adam Beneschan
  0 siblings, 0 replies; 17+ messages in thread
From: Adam Beneschan @ 2014-10-06 16:58 UTC (permalink / raw)


On Saturday, October 4, 2014 8:20:36 AM UTC-7, Dmitry A. Kazakov wrote:

> > On Saturday, October 4, 2014 10:32:44 AM UTC+2, Dmitry A. Kazakov wrote: 
> >> this case either. Assignment is not an operation in Ada.
> 
> > Just being picky on nomenclature: There is an *assignment statement* in
> > Ada, a part of that is the *assignment operation*.
> 
> > What you mean, is: Assignment is not an *operator* in Ada.
> 
> Not quite.
> 
> Though, I prefer "operator" to mean a lexical element of an expression
> (e.g. +) and "operation" to mean a subprogram taking the type as an
> argument or result (e.g. a primitive operation).

Actually, RM 3.2.3(1) considers both predefined operators (which are subprograms) and "language-defined operations such as assignment or a membership test" (which are not subprograms) to be "predefined operations", and it says those are included in "primitive operations".  

The AARM has this note (3.2.3(1.c)):

# Discussion: We use the term "primitive subprogram" in most of the
# rest of the manual. The term "primitive operation" is used mostly in
# conceptual discussions.

Whether this is actually true is debatable.  I tried searching for the term "primitive operation" in the AARM and found some places in 3.9 where it looks like the term was used where "primitive subprogram" would have been more appropriate, but those where only in the AARM, not the RM.  I didn't search really hard, though.  In any case, I can understand why there would be confusion.

                             -- Adam


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  2014-10-03 23:29 array of string Stribor40
  2014-10-04  0:50 ` Jerry
@ 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
  3 siblings, 1 reply; 17+ messages in thread
From: brbarkstrom @ 2014-10-07  1:06 UTC (permalink / raw)


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.


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  2014-10-03 23:29 array of string Stribor40
  2014-10-04  0:50 ` Jerry
  2014-10-07  1:06 ` brbarkstrom
@ 2014-10-07  2:08 ` Jerry
  2014-10-07 16:49 ` brbarkstrom
  3 siblings, 0 replies; 17+ messages in thread
From: Jerry @ 2014-10-07  2:08 UTC (permalink / raw)


with Ada.Strings.Unbounded;    use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
procedure Strings_Demo is
    type String_Array is array (Integer range <>) of Unbounded_String;
    Cities : String_Array(1 .. 2);
begin
    Cities(1) := To_Unbounded_String("London");
    Cities(2) := To_Unbounded_String("Toronto");
    Put_Line(Cities(1));
    Put_Line(Cities(2));
end Strings_Demo;

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Use of bounded strings? (Was: array of string)
  2014-10-07  1:06 ` brbarkstrom
@ 2014-10-07 16:06   ` Jacob Sparre Andersen
  0 siblings, 0 replies; 17+ messages in thread
From: Jacob Sparre Andersen @ 2014-10-07 16:06 UTC (permalink / raw)


Bruce <brbarkstrom@gmail.com> wrote:

> You could also create an array of Bounded_Strings.  [...]

Although I agree that Bounded_Strings is a possible solution for this
problem, I find that it is extremely rare (only in 0.3% of all the
source files I checked, and only in 2.7% of the string using source
files) that I end up using it.

How often do you use Bounded_Strings?

Greetings,

Jacob
-- 
"Nobody writes jokes in base 13."
 Douglas Adams


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: array of string
  2014-10-03 23:29 array of string Stribor40
                   ` (2 preceding siblings ...)
  2014-10-07  2:08 ` array of string Jerry
@ 2014-10-07 16:49 ` brbarkstrom
  3 siblings, 0 replies; 17+ messages in thread
From: brbarkstrom @ 2014-10-07 16:49 UTC (permalink / raw)


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?

Constantly.  It's a habit now.  One advantage is the Bounded_Strings
library in the ARM.  It has functions for appending strings, extracting slices,
obtaining single characters, and so on.  I've created a package I call
Common_Defs.ads that lets me have a moderately long string (256 characters)
and a very long one (5000 characters).  Note also that the Bounded_String
library throws an exception if you try to stuff too many characters into
the string.  That provides a reasonable way to filter out strings that could
cause buffer overflows in Web page text inputs.

Here's the code:

with Ada.Characters.Latin_1;
with Ada.Strings;
with Ada.Strings.Bounded;
with Ada.Numerics;
with Ada.Numerics.generic_elementary_functions;

package Common_Defs is
  ------------------------------------------------------------------------------
  -- Generic Packages
  ------------------------------------------------------------------------------
  subtype real        is long_float;
  package Usr_Math    is new
                        Ada.Numerics.generic_elementary_functions(real);
  Std_Vstring_Length  : constant :=  256;
  package VString     is new
                        Ada.Strings.Bounded.Generic_Bounded_Length(Std_Vstring_Length);
  Long_Vstring_Lngth  : constant := 5000;
  package Long_Vstring is new
                        Ada.Strings.Bounded.Generic_Bounded_Length(Long_Vstring_Lngth);
  ------------------------------------------------------------------------------
  -- Constant
  ------------------------------------------------------------------------------
   TAB                         : constant Character := Ada.Characters.Latin_1.HT;
end Common_Defs;

This package spec lets me create long_floats and bring along the appropriate
math functions in one fell swoop.  If using the compiler default on long_floats
worries you, you can define the numerical type you want and embed it in this
kind of spec.  The reason for having the TAB character is if you want to 
create TAB-delimited text files that you can feed into spreadsheets.  If you
do the usual 

with Common_Defs; use Common_Defs;

then you can just use Put(TAB) (or Put(Output_File, TAB)) in Text_IO 
interactions.

Bruce B.

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2014-10-07 16:49 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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