comp.lang.ada
 help / color / mirror / Atom feed
* Constrained Character Subtype
@ 1997-03-12  0:00 Tracy Fletcher
  1997-03-12  0:00 ` Matthew Heaney
  1997-03-13  0:00 ` Joakim Olsson
  0 siblings, 2 replies; 5+ messages in thread
From: Tracy Fletcher @ 1997-03-12  0:00 UTC (permalink / raw)



Howdy,

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:

type More_than_one_syllable is ("ana", "ani", "uno", etc.)
if INPUT_STRING(i..i+2) in More_than_one_syllable then . . .

I know this must be possible, but I have been able to find no effective
solution in reference books, internet documenets, etc.

Any advice would be greatly appreciated.

Thanks,

Tracy Fletcher




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

* Re: Constrained Character Subtype
  1997-03-12  0:00 Constrained Character Subtype Tracy Fletcher
@ 1997-03-12  0:00 ` Matthew Heaney
  1997-03-13  0:00   ` Jeff Carter
  1997-03-13  0:00   ` Robert Dewar
  1997-03-13  0:00 ` Joakim Olsson
  1 sibling, 2 replies; 5+ messages in thread
From: Matthew Heaney @ 1997-03-12  0:00 UTC (permalink / raw)



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
<mailto:matthew_heaney@acm.org>
(818) 985-1271




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

* Re: Constrained Character Subtype
  1997-03-12  0:00 Constrained Character Subtype Tracy Fletcher
  1997-03-12  0:00 ` Matthew Heaney
@ 1997-03-13  0:00 ` Joakim Olsson
  1 sibling, 0 replies; 5+ messages in thread
From: Joakim Olsson @ 1997-03-13  0:00 UTC (permalink / raw)



Tracy Fletcher <tfletch@primenet.com> wrote in article
<33270944.588B@primenet.com>...

The "TWO" in the if-statement below must be in uppercase so if you gonna
test a string you first have to convert it to UPPERCASE. this way you can
test diffrent strings or chatacter with Ada ENUMERATION-type, You can't use
numbers or '_' in the beginning of the value.
You can also try out the Ada.Strings.Unbounded std-package and with it
create a array of strings in different length.

PS. I've compiled and linked it, it works...

Se you in the Ada-web
/joakim the swed...



with Ada.Text_IO;

procedure test is
	type TST1 is ( one, two, three );
begin
	for I in TST1 loop	
		if "TWO" = TST1'image(I) then
			Ada.Text_IO.Put_Line("OK");
		end if;
	end loop;
end;






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

* Re: Constrained Character Subtype
  1997-03-12  0:00 ` Matthew Heaney
  1997-03-13  0:00   ` Jeff Carter
@ 1997-03-13  0:00   ` Robert Dewar
  1 sibling, 0 replies; 5+ messages in thread
From: Robert Dewar @ 1997-03-13  0:00 UTC (permalink / raw)



<<an incomplete version of this post got sent accidentally by noise on the
  line, I will cancel it, but if you see it, ignore it>>

Matthew Heany posts a longish note on how to deal with sets of characters.
I won't quote it, because it misses the very obvious answer:

  Sets of characters are a predefined feature in Ada 95!!!

Look up type Ada.Strings.Maps.Character_Set

a complete set of constructors, and functions to manipulate and provide
these sets is provided.

You can easily create the set Vowels using the To_Set operation, and
test for membership using the Is_In function.





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

* Re: Constrained Character Subtype
  1997-03-12  0:00 ` Matthew Heaney
@ 1997-03-13  0:00   ` Jeff Carter
  1997-03-13  0:00   ` Robert Dewar
  1 sibling, 0 replies; 5+ messages in thread
From: Jeff Carter @ 1997-03-13  0:00 UTC (permalink / raw)



Matthew Heaney wrote:
> You need some kind of set, because you're looking for some kind of testing
> membership.
> 
> There's a low-level way of implementing a set in Ada, by using a array of
> booleans:

Or you could use Ada.Strings.Maps.Character_Set;
-- 
Jeff Carter
Innovative Concepts, Inc.

Now go away, or I shall taunt you a second time.

Unsolicited commercial e-mail will be invoiced at $500 per piece.




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

end of thread, other threads:[~1997-03-13  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-03-12  0:00 Constrained Character Subtype Tracy Fletcher
1997-03-12  0:00 ` Matthew Heaney
1997-03-13  0:00   ` Jeff Carter
1997-03-13  0:00   ` Robert Dewar
1997-03-13  0:00 ` Joakim Olsson

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