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,a6beb80151a3525b X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Constrained Character Subtype Date: 1997/03/12 Message-ID: #1/1 X-Deja-AN: 225139924 References: <33270944.588B@primenet.com> Organization: Estormza Software Newsgroups: comp.lang.ada Date: 1997-03-12T00:00:00+00:00 List-Id: In article <33270944.588B@primenet.com>, tfletch@primenet.com wrote: >I am trying to define a type, which would be a subtype of CHARACTER. I >want it to contain a list of CHARACTER constants i.e. type VOWELS is >('a', 'e', 'i', 'o', 'u'); > >The problem is that I cannot compare one element of a string with type >VOWELS (compile error) for instance to count the vowels in a string >(i.e. if INPUT_STRING(j) in VOWELS . . .) I have tried making VOWELS a >subtype of CHARACTER, but cannot then specify the actual characters I >want to be included (aside from range, which does me no good). I would >also like to do something similar to recognize substrings such as: You need some kind of set, because you're looking for some kind of testing membership. If you have a contiguous range of characters, then you can use a built-in test: procedure Test (C : Character) is subtype Uppercase_Character is Character range 'A' .. 'Z'; begin if C in Uppercase_Character then ...; However, you can't do this for vowels, because that's a non-contiguous range. There's a low-level way of implementing a set in Ada, by using a array of booleans: procedure Test (C : Character) is type Character_Set is array (Character) of Boolean; Is_Vowel : constant Character_Set := ( 'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u' => True, others => False); -- I haven't tried compiling this... begin if Is_Vowel (C) then ... Please note that this is a low-level way of implementating a set. A better way is to write a generic set abstraction that takes a discrete formal type. (You would then instantiate the generic package using type Character.) Matt -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271