From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00,HK_RANDOM_FROM, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ccb8bd6b4c3162fd X-Google-Attributes: gid103376,public From: czgrr Subject: Re: Beginner's questions Date: 1999/05/04 Message-ID: <7gmhc3$saq$1@nnrp1.dejanews.com>#1/1 X-Deja-AN: 473844081 References: <372da49c.12366381@news.rwth-aachen.de> X-Http-Proxy: 1.0 x5.dejanews.com:80 (Squid/1.1.22) for client 193.192.234.4 Organization: Deja News - The Leader in Internet Discussion X-Article-Creation-Date: Tue May 04 10:15:33 1999 GMT Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT) Date: 1999-05-04T00:00:00+00:00 List-Id: 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