Reading Enumerated Types 
Author Message
 Reading Enumerated Types

Is there a simpler way to read user-defined types than reading it as
a string and then using a case statement to assign the appropriate
values? (Simply reading them normally worked fine in Pascal 4.0 but
when I try to compile on tp7 it tells me it can't read enumerated
types)
--
Danny Rathjens     'Verbing weirds language.'-- Calvin
GCS/M/S d-(+) s+:- a-- C++(+++)>$ USC+(++)>+++$ P+P>++ L E- W+

R++(+)* tv b+++ DI++++ D+(++) G e+>++ h r- y?



Wed, 18 Jun 1902 08:00:00 GMT  
 Reading Enumerated Types

Quote:
> Is there a simpler way to read user-defined types than reading it as
> a string and then using a case statement to assign the appropriate
> values? (Simply reading them normally worked fine in pascal 4.0 but
> when I try to compile on tp7 it tells me it can't read enumerated
> types)

   Pascal doesn't support i/o of enumerated data type variables; the
program(mer) must do any mapping of the standard data types upon which
i/o can be done - to the enumerated data variables/constants within the
program.  A Case statement is one good way to do this mapping, since
enumerated data declarations actually become ordinal values in a specific
data set. For instance, the following definition:
  Type Colors = (Red,Blue,Green,Yellow);
becomes an ordinal set (0,1,2,3), and external data will be these values
- and must be mapped.
   In reality, the purpose of enumerated data types is not so much to
read/write/store the values, but to provide greater compiler checking,
data typing, and documentation of the source code.


Wed, 18 Jun 1902 08:00:00 GMT  
 Reading Enumerated Types

There is a TP specific way of doing this, using typed constants. For
Example:

type Suits = (Club, Heart, Diamond, Spade);

const SuitText = array [Suits] of string[7] =
                 ('Club', 'Heart', 'Diamond', 'Spade');

var CardSuit : Suits;

begin



Wed, 18 Jun 1902 08:00:00 GMT  
 Reading Enumerated Types

Quote:

> Is there a simpler way to read user-defined types than reading it as
> a string and then using a case statement to assign the appropriate
> values? (Simply reading them normally worked fine in pascal 4.0 but
> when I try to compile on tp7 it tells me it can't read enumerated
> types)

Yes. Use a table and for loop.

type
  Foo = (Red, Green, Blue);

const
  FooStrings: array[Foo] of string = ('Red', 'Green', 'Blue');

function StrToFoo(Str: string): Foo;
var
  Index: foo;
begin
  for Index := Low(Foo) to High(Foo) do
    if Str = FooStrings[Index] then BREAK;
  Result := Index;
end;

--

http://www.midnightbeach.com/jon   Personal Pages
http://www.midnightbeach.com/jon/pubs Programming Publications
http://www.midnightbeach.com/hs             Home School Resource List



Wed, 18 Jun 1902 08:00:00 GMT  
 Reading Enumerated Types

Quote:

> > Is there a simpler way to read user-defined types than reading it as
> > a string and then using a case statement to assign the appropriate
> > values? (Simply reading them normally worked fine in pascal 4.0 but
> > when I try to compile on tp7 it tells me it can't read enumerated
> > types)

>    Pascal doesn't support i/o of enumerated data type variables; the
> program(mer) must do any mapping of the standard data types upon which
> i/o can be done - to the enumerated data variables/constants within the
> program.  A Case statement is one good way to do this mapping, since
> enumerated data declarations actually become ordinal values in a specific
> data set. For instance, the following definition:
>   Type Colors = (Red,Blue,Green,Yellow);
> becomes an ordinal set (0,1,2,3), and external data will be these values
> - and must be mapped.
>    In reality, the purpose of enumerated data types is not so much to
> read/write/store the values, but to provide greater compiler checking,
> data typing, and documentation of the source code.

Yes, that's what they were designed for. However, not rarely one does need to
input or output them in some way.

The following are generic I/O procedures. You'd still have to declare an array
for each enum type you want to use, but that's probably the best you can get
in TP7:

{ Copyright 1996 by Frank Heckenbach, free for non-commercial use }

{$X+ We need extended syntax}
USES Strings;

FUNCTION ReadEnum(Identifiers:ARRAY OF PChar):Word;
VAR
  s:ARRAY[Byte] OF Char;
  count,max:Word;
BEGIN
  max:=High(Identifiers);
  REPEAT
    Readln(s);
    count:=0;
    WHILE (count<=max) AND (StrIComp(s,Identifiers[count])<>0) DO Inc(count);
      {Use StrComp instead of StrIComp for case sensitivity.}
    IF count>max THEN Write('Unknown identifier! Try again: ')
  UNTIL count<=max;
  ReadEnum:=count
END;

PROCEDURE Wri{*filter*}um(Identifiers:ARRAY OF PChar;Value:Word);
BEGIN
  IF Value>High(Identifiers)
    THEN Writeln('Value out of range')
    ELSE Writeln(Identifiers[Value])
END;

{Up to here it's generic and can be put into a unit.
You can then use it like:}

TYPE TColor=(Red,Green,Blue);

CONST ColorIdentifiers:ARRAY[TColor] OF PChar=('Red','Green','Blue');

VAR Color:TColor;

BEGIN
  Write('Please enter a color: ');
  Color:=TColor(ReadEnum(ColorIdentifiers));
  Write('You entered ');
  Wri{*filter*}um(ColorIdentifiers,Ord(Color))
END.

The "trick" is that the "ARRAY OF" declaration automatically passes the size
of the actual parameter. This saves you from typing another parameter.
The PChar is preferable over a STRING[x] because you don't have to specify a
maximum length that would have to be the same for all types, in order to make
a generic procedure possible. And a STRING (length 255) would waste far too
much memory.

Of course you can modify the procedures above to take a string as a parameter
or return a string, rather than reading or writing...
Or change ReadEnum to trim the input line and to use substring matching and so
on - to be a bit more user friendly.

Often when an enum type is to be read, this can be done more comfortably with
a menu. The menu procedure can, of course, be quite similar to ReadEnum.
In fact, it could use the same header as ReadEnum.

Hope this helps,
Frank



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Best Way to Select From Enumerated Type

2. enumerated & subrange types

3. Random enumerated type.

4. Need Help On Enumerate Types

5. Procedure wont save using enumerated types

6. How does the new enumerated type fkInternalCalc work?

7. Enumerated and subrange type

8. Enumerated type in a pascal compiler

9. Enumerated types

10. Converting Enumerated types

11. Enumerated data type HELP!!

12. enumerated type problem

 

 
Powered by phpBB® Forum Software