comp.lang.ada
 help / color / mirror / Atom feed
From: czgrr <czgrr@my-dejanews.com>
Subject: Re: Beginner's questions
Date: 1999/05/04
Date: 1999-05-04T00:00:00+00:00	[thread overview]
Message-ID: <7gmhc3$saq$1@nnrp1.dejanews.com> (raw)
In-Reply-To: 372da49c.12366381@news.rwth-aachen.de

In article <372da49c.12366381@news.rwth-aachen.de>,
  marcoschmidt@geocities.com wrote:
[snip]
> 2) I want to use a fixed-size array of constant strings to store some
> names which do not all have the same length, but no name is longer
> than 20 characters. My approach looks like this:
>
>   type t_my_array is array(0..17) of String;
>
>   names : constant t_my_array := ("string 1", "string 2", ...,
>     "string 18");
>
> The compiler doesn't like this (unconstrained element type). If I
> replace String with String(0..19), it is compiled, but a runtime error
> is raised. How should I do it?
>
> Thanks in advance,
> Marco
Hi Marco.

P.S. I am using Ada83 and don't know Ada95. If you are using the latter, no
doubt someone else here will correct what I might get wrong.

Several perfectly good solutions have already been posted in response to this
message. I have yet another solution, but first...

1. The reason that you are getting the unconstrained element type is that you
cannot declare an array (or indeed a variable) of type STRING without
specifying a length. Constants can be declared, but only because the length is
implicitly determined:
  temp : CONSTANT STRING := "Hello there." ;
In the above example, you could say:
  SUBTYPE string_20 IS STRING ( 1 .. 20 ) ;
  TYPE t_my_array IS ARRAY ( 0 .. 17 ) of string_20 ;
which is essentially the same as your String(0..19) but see below.

2a. The reason that you get the runtime error is that the index to STRING is
POSITIVE. So 'STRING ( 0 .. 19 )' is an error but 'STRING ( 1 .. 20 )' is not,
because 0 is not a positive number (btw, STRING ( 1 .. 0 ) is allowed).

2b. You will also get a runtime error because the strings you are assigning to
the array are not each 20 characters long:
  names : CONSTANT t_my_array :=
    ( "string 1            ",
      "string 2            ",
      ...,
      "string 18           " ) ;

Now, another alternative solution...

When I learned Pascal at University, sometimes the format of the output was
constructed using the knowledge that the strings being printed were a fixed
length. This is useful for printing columns of data, formatted text files,
etc. Now there are other ways of doing this in Ada anyway using TEXT_IO, but
there are also other reasons why strings of fixed lengths are required. You
said yourself in your message that each string is no more than 20 characters
long, so presumably there would be a reason for this.

So an alternative solution is to use the above type declaration for string_20
and a couple of simple functions:

  -- This puts the first 20 characters of text into a string_20 type.
  -- If text is shorter than 20 characters, spaces are added to the end.
  FUNCTION padded_string
             ( text : IN     STRING )
                      RETURN string_20 ;

  -- This removes any trailing spaces from the end of text.
  FUNCTION trunc_string
             ( text : IN     string_20 )
                      RETURN STRING ;

Then, to continue your example, you can do the following:
  names : CONSTANT t_my_array :=
    ( padded_string ( "string 1" ),
      padded_string ( "string 2" ),
      ...
      padded_string ( "string 18" ) ) ;
and perhaps:
  FOR i IN 0 .. 17 LOOP
    TEXT_IO.PUT_LINE
      ( "Orig. String is '" & names ( i ) & "'." ) ;
  END LOOP ;
  FOR i IN 0 .. 17 LOOP
    TEXT_IO.PUT_LINE
      ( "Trunc string is '" & trunc_string ( names ( i ) ) & "'." ) ;
  END LOOP ;

It all depends what you want from the program.

HTH
czgrr

--
My opinions, suggestions, etc, are not necessarily those of my employer.
They might not even be right. Use at your own risk.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




  parent reply	other threads:[~1999-05-04  0:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-05-02  0:00 Beginner's questions Marco Schmidt
1999-05-02  0:00 ` Matthew Heaney
1999-05-03  0:00   ` Marco Schmidt
1999-05-03  0:00     ` Matthew Heaney
1999-05-03  0:00 ` dennison
1999-05-04  0:00   ` Tucker Taft
1999-05-04  0:00     ` dennison
1999-05-10  0:00       ` Tucker Taft
1999-05-03  0:00 ` Andreas Winckler
1999-05-03  0:00   ` Matthew Heaney
1999-05-03  0:00     ` Andreas Winckler
1999-05-03  0:00       ` David Starner
1999-05-04  0:00         ` Andreas Winckler
1999-05-04  0:00           ` Larry Kilgallen
1999-05-04  0:00             ` Martin C. Carlisle
1999-05-04  0:00               ` Larry Kilgallen
1999-05-05  0:00                 ` Andreas Winckler
1999-05-04  0:00 ` czgrr [this message]
1999-05-04  0:00   ` Nick Roberts
1999-05-07  0:00     ` Robert Dewar
1999-05-07  0:00       ` David Starner
1999-05-08  0:00         ` ak
replies disabled

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