Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any simplier way to 'read' enumerated types in pascal?

Tags:

enums

pascal

User inputs an integer which corresponds to a value of defined enumerated type. I need to assign that value to variable t. This is what I thought of:

type test = (red,green,blue,fish);
var t:test;
    n,i:integer;
begin
  readln(n);
  t:=red;
  for i:=1 to n do
    t:=succ(t);
end.

Am I overcomplicating the task? Is it possible to write a simpler alogrithm?

like image 551
user1242967 Avatar asked Sep 05 '25 03:09

user1242967


1 Answers

You should be able to just cast the integer to the enumerated type, for example:

t := test(n);

If you want to go the other way, then use ord:

n := ord(t);

That should let you move numerically to any item in the list. You can check the bounds with:

Ord(Low(test)))

and

Ord(High(test))

..where test is your type.

like image 118
Geoff Avatar answered Sep 07 '25 21:09

Geoff