String Arrays in Ada - arrays

I have a program in Ada95, in which I have to create an array of strings. This array can contain strings of variable length.
Example:
I have declared the array in which all the indexes can store strings of size 50. When I assign a smaller string to the above array, I get "Constraint Error".
Code:
procedure anyname is
input_array : array(1..5) of String(1..50);
begin
input_array(1):="12345";
end anyname;
I have tried to create the array of Unbounded_Strings. But that doesn't work either. Can anyone tell me how to store this "12345" in the above string array?

If you use Unbounded_String, you cannot assign a string literal to it directly. String literals can have type String, Wide_String, or Wide_Wide_String, but nothing else; and assignment in Ada usually requires that the destination and source be the same type. To convert a String to an Unbounded_String, you need to call the To_Unbounded_String function:
procedure anyname is
input_array : array(1..5) of Ada.Strings.Unbounded.Unbounded_String;
begin
input_array(1) := Ada.Strings.Unbounded.To_Unbounded_String ("12345");
end anyname;
You can shorten the name by using a use clause; some other programmers might define their own renaming function, possibly even using the unary "+" operator:
function "+" (Source : String) return Ada.Strings.Unbounded.Unbounded_String
renames Ada.Strings.Unbounded.To_Unbounded_String;
procedure anyname is
input_array : array(1..5) of Ada.Strings.Unbounded.Unbounded_String;
begin
input_array(1) := +"12345"; -- uses renaming "+" operator
end anyname;
Not everyone likes this style.

You can use Ada.Strings.Unbounded, illustrated here, or you can use a static ragged array, illustrated here. The latter approach uses an array of aliased components, each of which may have a different length.
type String_Access is access constant String;
String_5: aliased constant String := "12345";
String_6: aliased constant String := "123456";
String_7: aliased constant String := "1234567";
...
Input_Array: array (1..N) of
String_Access :=
(1 => String_5'Access,
2 => String_6'Access,
3 => String_7'Access,
-- etc. up to N
);

Strings in Ada are arrays of characters of fixed length. In order to use strings of variable length (which may often be the case when arrays of strings are needed, e.g. arrays of names, each name being of variable length), each individual string may be declared as an Unbounded_String. The only caveat is that this allocates from the heap memory. Below is a complete example of an array of strings in Ada.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;
procedure arrayAda is
type DaysArray is array(1..7) of Unbounded_String;
days: DaysArray;
begin
days(1):=To_Unbounded_String("Sunday");
days(2):=To_Unbounded_String("Monday");
days(3):=To_Unbounded_String("Tuesday");
days(4):=To_Unbounded_String("Wednesday");
days(5):=To_Unbounded_String("Thursday");
days(6):=To_Unbounded_String("Friday");
days(7):=To_Unbounded_String("Saturday");
for index in 1..7 loop
Put(days(index));
Put(" ");
end loop;
end arrayAda;
This produces the following output:
$ ./arrayAda
Sunday Monday Tuesday Wednesday Thursday Friday Saturday

I've had a lot of joy from instantiating a container package, e.g.:
package String_Vectors is
new Ada.Containers.Indefinite_Vectors (Positive, String);
It's still a bit fiddly, compared to how easy it is to mess about with strings in a lot of other programming languages, but it's okay.
Fundamentally, Ada is a language designed to be usable without using the heap (at all :-) Most other languages would fall down in a, well, a heap, without the heap.

Related

Unable to print the contents of an Array String of String Constants?

I'm a bit of a beginner when it comes to Ada, and I'm trying to declare and use an array of strings of different lengths.
Using Ada'83 I can declare an array of variable length string constants as follows (example taken from the Ada FAQ)
type table is access String;
TESTS : constant array (Positive range 1..3) of table
:= ( 1 => new String'("One"),
2 => new String'("Two"),
3 => new String'("Three")
);
However much to my frustration even though the result appears to be an array of character arrays they don't behave as strings. When I try to compile the following code I get an error message 'Inconsistency detected during overload resolution [LRM 8.7]'
for COUNT in TESTS'Range loop
Put(TESTS(COUNT));
New_Line;
end loop;
However, I can print out the content of each of the 'strings' using the following code.
for COUNT in TEST'Range loop
for COUNTER in TEST(COUNT)'Range loop
Put(TEST(COUNT)(COUNTER));
end loop;
New_Line;
end loop;
Unfortunately I want to use the values to test some code that takes a string as a parameter, so this doesn't really help...
Is there a way to be to iterate over an array of string constants of varying length in Ada'83, or to convert the character arrays into strings of varying length.
Thanks
No, this isn't homework, and yes, I know I'm using an ancient compiler!
Test is undefined; I'll presume you mean Tests.
Table is not a string type; it is an access type. To reference the value that an access value designates, one uses .all:
Tests (Tests'First).all
is a string. Ada contains some shortcuts for access-to-array types to make them easier to use, allowing .all to be left off before attributes and indexing, which is why Tests (Count)'Range and Tests (Count) (Counter) work. To reference the whole value, though, .all is required:
Text_IO.Put_Line (Item => Tests (Counter).all);
However, a better approach would be to define a variable-length string abstraction and use that instead of an access type.
Thank you that works a treat - however, how would I go about defining
'a variable-length string abstraction' to do the same job?
Use private-types + access-types, perhaps like the following:
Package String_Abstraction is
Type DString is private;
Function "+"( Right : DString ) return String;
Function "+"( Right : String ) return DString;
Function "&"( Left, Right : String ) return DString;
--...
Private
Type Data(<>);
Type DString is access Data;
End String_Abstraction;
with implementation of:
Package Body String_Abstraction is
Type Data( Length : Natural ) is record
Text : String(1..Length) := (others => ASCII.NUL);
end record;
Function "+"( Right : String ) return DString is
Begin
Return New Data'( Text => Right, Length => Right'Length );
End "+";
Function "&"( Left, Right : String ) return DString is
Begin
Return +(Left & Right);
End "&";
Function "+"( Right : DString ) return String is
Begin
Return Right.Text;
End "+";
--...
End String_Abstraction;
Which could be used as follows:
Table : Constant Array(Positive range <>) of String_Abstraction.DString:=
( String_Abstraction."+"( "This" ),
String_Abstraction."+"( "EXAMPLE" ),
String_Abstraction."+"( "list" ),
String_Abstraction."+"( "exists." )
);
and
Print_Table:
For Index in Table'Range Loop
Declare
Use String_Abstraction;
Item : DString renames Table(Index);
Begin
Ada.Text_IO.Put_Line( +Item );
End;
End loop Print_Table;
If you had a use prior to the declaration of table, you could have:
Use String_Abstraction;
Table : Constant Array(Positive range <>) of String_Abstraction.DString:=
( +"This",
+"EXAMPLE",
+"list",
+"exists."
);
It's certainly not complete, but that gives you the basic idea of how to do it.
GNAT Studio Community Edition is free to use for freelance use and hobby going coders. It comes pre-installed with a decent Ada 2012 compiler. I had to also install a small add on called Ada GDE to get the code to run.
If your looking for an up to date book to study from, I highly recommend Ada 2012 by john Barnes. It's a bit of a beast at over 900 pages long, but he goes into good detail on concepts and provides plenty of sample code to help. He also wrote one for Ada 95 I believe.

How do you initialize an array with only one element, without hardcoding the first index?

I am trying to pass an array of Unbounded_String to a function, and I don't care about the range of the index, as the function is going to loop over each element.
The (element1, element2) syntax automatically starts at the first index value in the range, then increments for the second value given, which works fine for more than one value. However, for a single value, this cannot be used as the parentheses are considered superfluous.
This code shows the error messages for each of the attempts I have made. (1) works, but (2), the preferable syntax for passing a single-element array, does not. (3) works, and is given as an answer to this similar question. However, this hardcodes the first index of the range into the calling side; if the String_Array implementation changes, all the call-sites have to be changed, even though they don't care about the index values used.
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
procedure Main is
function "+"(S: String) return Ada.Strings.Unbounded.Unbounded_String
renames Ada.Strings.Unbounded.To_Unbounded_String;
type String_Array is array (Positive range <>) of Unbounded_String;
procedure Foo(input : in String_Array) is
begin
null;
end Foo;
begin
Foo((+"one", +"two")); --(1)
--Foo((+"only")); --(2) positional aggregate cannot have one component
Foo((1 => +"only")); --(3)
--Foo((String_Array'First => +"only")); --(4) prefix for "First" attribute must be constrained array
--Foo((String_Array'Range => +"only")); --(5) prefix for "Range" attribute must be constrained array
--Foo((String_Array'Range'First => +"only")); --(6) range attribute cannot be used in expression
--Foo((String_Array'Range'Type_Class'First => +"only")); --(7) range attribute cannot be used in expression
end Main;
What you want (2) is indeed impossible as it could be mistaken for a parenthesized expression (see http://www.adaic.org/resources/add_content/standards/12aarm/html/AA-4-3-3.html note 10).
If you really want to avoid expression (3) for the reasons you stated, as workaround, you could define a function to handle the one-element array case:
function Singleton_String_Array (S: String) return String_Array is ((1 => + S));
-- one element call
Foo(Singleton_String_Array ("only"));
It reuse your expression (3) but the first index hardcoding is no longer done on call site.
You can also overload your foo function to handle the special one-element case:
procedure Process_String (input : in Ada.Strings.Unbounded.Unbounded_String) is
begin
null;
end Process_String;
procedure Foo(input : in String_Array) is
begin
for string of input loop
Process_String (string);
end loop;
end Foo;
procedure Foo(input : in Ada.Strings.Unbounded.Unbounded_String) is
begin
Process_String (input);
end Foo;
-- One element call
Foo(+"only");
The short answer is that all array objects must be constrained, which means callers usually have to decide on the array bounds.
However, you know the index type, and could do
Foo((Positive'First => +"only"));
which doesn't really answer your question, since someone may still fiddle with the array range, and there's not really any guard against that.
Adding a new subtype as the range may be a viable solution, though:
subtype String_Array_Range is Positive;
type String_Array is array (String_Array_Range range <>) of Unbounded_String;
...
Foo((String_Array_Range'First => +"only"));
Any fiddling can now be done on the String_Array_Range subtype without affecting any callers. But there's still no guarantee against evil programmers changing the index type of the array itself...
type String_Array is array (Positive range <>) of Unbounded_String;
Declares a type of array but doesn't provide the size.
Remember that an array has a static size.
So String_Array'First and String_Array'Range don't match to anything.
If you declared
type my_String_Array is String_Array(1 .. 35);
my_arr : my_String_Array
Then my_arr'First denotes 1 and my_arr'Range denotes 1..35.
As long as you don't put a contraint on the type, you won't have access to these attributes.

Array of strings in Ada record

I have the following record in some Ada code:
type My_Type (Width, Height : Positive) is
record
Data : array (1 .. Height) of String (1 .. Width);
-- Some other stuff
end record;
Of course I can't have anonymous arrays in my record, but I can't come up with a way to name the array beforehand.
I know that I could make the package depend on width and height to redefine the string of the correct length and name an array of them, but that makes it clunky to have many different sized records at once (which I want).
Does anybody know of a more elegant solution to the problem?
Note: a 2-D array of characters would work, but only if I could extract strings from it in a straightforward way, which is again something I'm not sure how to do.
There are several possible solutions to your problem, including the use of unbounded strings. Below is a solution using a generic package:
generic
width : Positive;
height : Positive;
package Gen_String_Matrix is
Subtype My_String is String(1..width);
type My_Matrix is array(1..height) of My_string;
end Gen_String_Matrix;
An example of using this package is:
with Ada.Text_IO; use Ada.Text_IO;
with gen_string_matrix;
procedure Gen_String_Main is
package My_Matrices is new Gen_String_Matrix(width => 3,
height => 10);
use My_Matrices;
Mat : My_Matrices.My_Matrix;
begin
for Str of Mat loop
Str := "XXX";
end loop;
for Str of Mat loop
Put_Line(Str);
end loop;
end Gen_String_Main;

Ada constant array of string literals

I have a large array in C that I wish to move into my ada project. That array is used to store filenames for assets which will later be loaded. It looks something like:
const char *filenames[NUMBER_OF_FILES] = {
/* file[0] */ "res/0.file",
/* ... */
/* file[n] */ "res/some_more/complicated/file.name"
};
I want to move this into an ada package body, but can't find a decent way to do it. Obviously my first attempt was:
filenames : constant array (File_Index) of String := (
index_0 => "res/0.file",
-- ...
index_n => "res/some_more/complicated/file.name"
);
But of course String is an unconstrained type, so Ada won't allow that. I switched it to use Unbounded_Strings, which worked, but was very ugly (having to wrap each string with To_Unbounded_String.
Is there any way to make an array of unconstrained types whose size will be known at compile time like this, or do I have to use unbounded strings?
It’s a bit low-level and repetitive, but perhaps you can create a little program (maybe even in Ada!) to generate something like
with Ada.Text_IO; use Ada.Text_IO;
procedure Lambdabeta is
F1 : aliased constant String := "res/0.file";
F2 : aliased constant String := "res/some_more/complicated/file.name";
type Strings is array (Positive range <>) of access constant String;
Filenames : constant Strings := (F1'Access,
F2'Access);
begin
for J in Filenames'Range loop
Put_Line (Filenames (J).all);
end loop;
end Lambdabeta;
See also this answer on minimising the pain of using To_Unbounded_String.
Arrays can't contain objects of indefinite types.
Basically you have two options:
Use another container than an array.
Encapsulate the strings in a definite type.
So you could use Ada.Containers.Indefinite_Vectors instead of an array:
with Ada.Containers.Indefinite_Vectors;
package Settings is
------------------------------------------------------------------
-- You may want to put this block in a separate package:
package String_Vectors is
new Ada.Containers.Indefinite_Vectors (Index_Type => Positive,
Element_Type => String);
function "+" (Left, Right : String) return String_Vectors.Vector
renames String_Vectors."&";
function "+" (Left : String_Vectors.Vector;
Right : String) return String_Vectors.Vector
renames String_Vectors."&";
------------------------------------------------------------------
File_Names : constant String_Vectors.Vector :=
"/some/file/name" +
"/var/spool/mail/mine" +
"/etc/passwd";
end Settings;
So you could use Ada.Strings.Unbounded.Unbounded_String instead of String:
with Ada.Strings.Unbounded;
package Settings_V2 is
function "+" (Item : in String) return Ada.Strings.Unbounded.Unbounded_String
renames Ada.Strings.Unbounded.To_Unbounded_String;
type String_Array is array (Positive range <>)
of Ada.Strings.Unbounded.Unbounded_String;
File_Names : constant String_Array :=
(+"/some/file/name",
+"/var/spool/mail/mine",
+"/etc/passwd");
end Settings_V2;
What has not yet been mentioned is that you can just use a function:
subtype File_Index is Integer range 1 .. 3;
function Filename (Index : File_Index) return String is
begin
case Index is
when 1 => return "res/0.file";
when 2 => return "res/1.file";
when 3 => return "res/some_more/complicated/file.name";
end case;
end Filename;
Using Filename (1) in your code is identical to accessing an array element.

Access first element of string in Ada

I have a string passed into a function, I would like to compare the first character of the string against a number.
I.E.
if String(1) = "3" then
When I compile I get:
warning: index for String may assume lower bound of 1
warning: suggested replacement String'First + 1
I would really like to make this right, but when I try "first" it actually grabs a number, not the character.
Is there a better way to do it?
I tried looking up the 'First concept, and the below site explains I'm actually getting the number of the index, not the actual contents: http://en.wikibooks.org/wiki/Ada_Programming/Types/array
For example,
Hello_World : constant String := "Hello World!";
World : constant String := Hello_World (7 .. 11);
Empty_String : constant String := "";
Using 'First I'll get:
Array 'First 'Last 'Length 'Range
Hello_World 1 12 12 1 .. 12
World 7 11 5 7 .. 11
Empty_String 1 0 0 1 .. 0
Based on that information, I can't get H from Hello world (for a comparison like if Hello_World(1) = "H" then)
EDIT:
So the way I initially was doing it was
(insert some variable name instead of string in this case)
String(String'First .. String'First) = "1"
So that works from what I can tell, however, rather then writing all that, I found out that
String(String'First) = '1'
Does the same thing but using char comparison, which makes a lot more sense!
Thanks for all the answers everyone!
Strings are the biggest bugaboo for newbie Ada coders; particularly so for those who are already experts at dealing with strings in Cish languages.
Ada strings (in fact all Ada arrays) are not 0 based like C, or 1-based like Fortran. They are based however the coder felt like it. If someone wants to index their string from 10 ... 200, they can. So really the safest way to acces characters in an Ada string is to use the 'first attribute (or better yet, loop through them using 'range or 'first .. 'last).
In your case it looks like you want to get at only the first character in the string. The easiest and safest way to do that for a string named X is X(X'first).
In pactice you would almost never do that though. Instead you would be looping through the string's 'first...'last looking for something, or just using one of the routines in Ada.Strings.Fixed.
The warning is suggesting you use:
String(String'First + Index)
Instead of just
String(Index)
There's something odd about the code in your question. First off, that you're calling your variable "String" and that it's of type "String". Ada will balk at that right off the bat.
And the warning statements you reproduce for that code fragment don't make sense.
Let's say your variable is actually called "Value", i.e.:
Value : String := "34543";
Value(1) is not the same as Value(Value'First + 1), because Value'First (in this declaration) is 1. So you end up referencing Value(1 + 1). You appear to be experiencing this because of mentioning that you can't reference the 'H' in a "Hello World" string.
Now the warning is valid, in that you're safer using 'First (and 'Last and 'Range) to reference array bounds. But you need to use the proper indexing if you're going to offset from the bound retrieved via 'First, typically using either 0-based or 1-based (in which case you need to offset by 1). Use whichever base is more appropriate and readable in your context.

Resources