comp.lang.ada
 help / color / mirror / Atom feed
* Re: how to create an array of N elements?
       [not found] <c923f575.0208170639.344c3c21@posting.google.com>
@ 2002-08-17 16:16 ` Warren W. Gay VE3WWG
       [not found]   ` <c923f575.0208191223.6073ebf4@posting.google.com>
  0 siblings, 1 reply; 4+ messages in thread
From: Warren W. Gay VE3WWG @ 2002-08-17 16:16 UTC (permalink / raw)


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




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

* Re: how to create an array of N elements?
       [not found]   ` <c923f575.0208191223.6073ebf4@posting.google.com>
@ 2002-08-20  1:28     ` Richard Riehle
  2002-08-20 15:08     ` Stephen Leake
  1 sibling, 0 replies; 4+ messages in thread
From: Richard Riehle @ 2002-08-20  1:28 UTC (permalink / raw)


drmed wrote:

> thank you.
>
> then an other question:
>
> list : unbounded_string;
> type page is array(Positive range <>) of list;
> text : page(1 .. 300);
>
> but how can I change the number of elements in runtime? for example I
> delete a element of the array text or add one so that the array text
> now has only 200 elements and really only 200 elements and not contain
> a null string.

Consider using a declare block.  For example,

    with ada.integer_text_io;
    procedure Dynamic_Array is
        Size : Positive := 300;
    begin
        ada.integer_text_io.get(size);
       Variable_Size_Array_Block:
       declare
           S : String(1..Size);
       begin
          for I in S'Range
             loop
               null;
             end loop;
      end Variable_Size_Array_Block;
end Dynamic_Array;

Many Ada developers will recommend a better
approach such as nesting a subprogram in the
declarations of the enclosing unit and passing the
new size as a parameter.   Either way, you can
accomodate variable size array declarations in
your program if you need them.

Richard Riehle





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

* Re: how to create an array of N elements?
       [not found]   ` <c923f575.0208191223.6073ebf4@posting.google.com>
  2002-08-20  1:28     ` Richard Riehle
@ 2002-08-20 15:08     ` Stephen Leake
       [not found]       ` <3d63525a_4@news.bluewin.ch>
  1 sibling, 1 reply; 4+ messages in thread
From: Stephen Leake @ 2002-08-20 15:08 UTC (permalink / raw)


jonas.gasser@dataflow.ch (drmed) writes:

> thank you.
> 
> then an other question:
> 
> list : unbounded_string;
> type page is array(Positive range <>) of list;
> text : page(1 .. 300);

This is not legal Ada; "list" is an object, not a type.

Perhaps you should pick a less ambitious project while you are still
learning Ada?

> but how can I change the number of elements in runtime? for example
> I delete a element of the array text or add one so that the array
> text now has only 200 elements and really only 200 elements and not
> contain a null string.

You need an unbounded container package. See the Booch components
(http://www.adapower.com/booch/overview.html), or SAL
(http://users.erols.com/leakstan/Stephe/Ada/sal.html), or others at
http://www.adapower.com/

> 
> 
> jonas

-- 
-- Stephe



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

* Re: how to create an array of N elements?
       [not found]       ` <3d63525a_4@news.bluewin.ch>
@ 2002-08-26 18:11         ` Robert A Duff
  0 siblings, 0 replies; 4+ messages in thread
From: Robert A Duff @ 2002-08-26 18:11 UTC (permalink / raw)


"Jonas Gasser" <jonas.gasser@dataflow.ch> writes:

> my editor is finished, but in the moment I use for saving the lines an array
> with 1 .. 8000 elements, with or without a string inside -> and I think that
> this isn't very convenient.
> that's why I'm need a better solution.

Here's a reasonable data structure for an editor: Create an array
somewhat bigger than the size of the file being edited.  Store the
characters in that array, with a "hole" in the middle.  The hole is the
current point of insertion/deletion (where the "cursor" is on the
screen).  So if the file contains "Hello, world.", the array might
contain:

    Hello, woXXXXXrld.

Where "XXXXX" represents the hole.  The user is currently editing at the
point between "o" and "r".  If the user moves the cursor forward one,
the array becomes:

    Hello, worXXXXXld.

I.e., copy the "r" to the left of the hole.  (I don't mean to imply that
the X's are actually stored -- the contents of the hole are irrelevant.)

Inserted characters shrink the hole.  When the hole disappears, allocate
a bigger array.  So you really need a pointer-to-array (since the thing
can grow and shrink), plus a couple of integers indicating the hole
boundaries.

Obviously, no such data structure is built in to a general-purpose
language like Ada.  You need to make it a private type, completed by a
record type containing the appropriate stuff, and define operations for
insertion, deletion, cursor movement, etc.

- Bob



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

end of thread, other threads:[~2002-08-26 18:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <c923f575.0208170639.344c3c21@posting.google.com>
2002-08-17 16:16 ` how to create an array of N elements? Warren W. Gay VE3WWG
     [not found]   ` <c923f575.0208191223.6073ebf4@posting.google.com>
2002-08-20  1:28     ` Richard Riehle
2002-08-20 15:08     ` Stephen Leake
     [not found]       ` <3d63525a_4@news.bluewin.ch>
2002-08-26 18:11         ` Robert A Duff

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