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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,754e583fcabdfad2 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Help Date: 1996/11/19 Message-ID: #1/1 X-Deja-AN: 197483301 distribution: world references: <328C2BF5.1348@informatik.tu-muenchen.de> content-type: text/plain; charset=ISO-8859-1 organization: Estormza Software mime-version: 1.0 newsgroups: comp.lang.ada Date: 1996-11-19T00:00:00+00:00 List-Id: In article <328C2BF5.1348@informatik.tu-muenchen.de>, "Alexander B. Schmidt" wrote: >Hi, > >does anybody know how to declare a string of variable length in Ada >83? Declaring a variable of type String like >str: STRING(1..20); >and assigning a string like >str := "a String" >results in a constraint error if the length of the string is not equal >to 20. >Is it possible to declare strings with a maximum length which accept >the assignment of shorter strings too? The technique I often use in Ada 83 is to use a discriminated record, as follows: subtype String_Buffer_Length_Range is Natural range 0 .. 20; type String_Buffer (Length : String_Buffer_Length_Range := 0) is record Text : String (1 .. Length); end record; function "+" (Text : String) return String_Buffer is subtype Slided is String (1 .. Text'Length); begin return (Text'Length, Slided (Text)); end; Now, given those definitions, I can do this: declare The_String : String_Buffer; begin The_String := +"Robert Dewar"; The_String := +"Norm"; The_String := +"Ada"; When you refer to the string, you don't need to apply an index constraint, ie Put_Line (The_String.Text); -- The_String.Length worth of characters, not 20 This is also how to make a ragged array in Ada 83; just declare an array of String_Buffer. M. -------------------------------------------------------------------- Matthew Heaney Software Development Consultant mheaney@ni.net (818) 985-1271