comp.lang.ada
 help / color / mirror / Atom feed
From: "Warren W. Gay VE3WWG" <ve3wwg@cogeco.ca>
Subject: Re: how to create an array of N elements?
Date: Sat, 17 Aug 2002 12:16:03 -0400
Date: 2002-08-17T12:16:03-04:00	[thread overview]
Message-ID: <3D5E76C3.1070005@cogeco.ca> (raw)
In-Reply-To: c923f575.0208170639.344c3c21@posting.google.com

This should be called a RFAQ question (Real Frequently AQ) ;-)

drmed wrote:
> hi,
> my code:
> 
> procedure main is
> type abc is array(Positive range <>) of Character;
> letters : abc(1 .. 26)
> begin
> end main;
> 
> now in my code, letters is an array with 26 elements.
> how can I do it, that the size of the array changes in runtime?
> sometimes only 20 elements, then add one element, delete one and etc.
> jonas

This was just recently rehashed days ago, as this question so often is
here. To requote myself from a prior posting (an example that starts
with a 10 character array, then progressing to other lengths):

with Ada.Text_IO;

procedure EG is
     use Ada.Text_IO;

     function Length(S : String) return Natural is
     begin
        return S'Length;
     end Length;

     S :      String(1..10) := "1234567890";
     Last :   Natural := S'Last;
begin
     Put_Line("S='" & S(1..Last) & "'");

-- Last       := 3; -- or..
     Last       := Length("Cat");  -- Because can't use "Cat"'Last
     S(1..Last) := "Cat";

     Put_Line("S='" & S(1..Last) & "'");

end EG;

The idea is that you work with a variable called Last
(or something like it). If you don't like counting
characters in a larger constant like "Some message..."
then the use of a Lenght() function can be helpful (see
the statement Last := Length("cat");). Unfortunately,
doing "Some very long string constant"'Length is not legal.

At other times, you declare the variable when you
know its length, like:

declare
     My_New_String : String(1..Computed_Length);
     Another_String : String := String_1 & String_2;
begin
     ...

Or you have a function return the exact length string
you need, as in:

declare
     Returned_String : String := My_Function(whatever);
begin
     ...

and then thow it away when you're done with it using:

end;

of the declare..begin..end block. You can do this for
each iteration within a loop as well.

This requires a little different planning than C programmers
are used to. But once you catch onto the general paradigm
shift, you'll find that Ada fixed strings, packages
Ada.Strings.Fixed and Ada.Characters.Handling cover most
of your string needs.

If you deal with a number of variable length strings, then
sometimes resorting to Ada.Strings.Unbounded makes your
life easier as many have already pointed out. However, I
find that once you cross over to Ada.Strings.Unbounded, then
other features of these strings become "less natural" (for
example you must use a Length function instead of a Length
attribute, and slices become more of a nuisance etc.)

-- 
Warren W. Gay VE3WWG
http://home.cogeco.ca/~ve3wwg




       reply	other threads:[~2002-08-17 16:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <c923f575.0208170639.344c3c21@posting.google.com>
2002-08-17 16:16 ` Warren W. Gay VE3WWG [this message]
     [not found]   ` <c923f575.0208191223.6073ebf4@posting.google.com>
2002-08-20  1:28     ` how to create an array of N elements? Richard Riehle
2002-08-20 15:08     ` Stephen Leake
     [not found]       ` <3d63525a_4@news.bluewin.ch>
2002-08-26 18:11         ` Robert A Duff
replies disabled

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