baldwin@usna.MIL (J.D. Baldwin) (12/01/89)
Is there a good, clean way to convert the value of an enumerated ordinal type to a string value? That is, what does the function ConverterFunction(somevalue:sometype) : string look like in this fragment: type veggies = (carrots, limabeans, beets, toadstools); . . . var whatveg : veggies; . . . whatveg := carrots; . . . writeln(ConverterFunction(whatveg)); so that the writeln statement will output the string "carrots"? This question is not exactly mission-critical to me, but an answer would be nice. If replies are by e-mail, I'll summarize. -- From the catapult of: |+| "If anyone disagrees with anything I _, J. D. Baldwin, Comp Sci Dept |+| say, I am quite prepared not only to __||____..}-> US Naval Academy |+| retract it, but also to deny under \ / baldwin@cad.usna.navy.mil |+| oath that I ever said it." --T. Lehrer ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jrwsnsr@nmtsun.nmt.edu (Jonathan R. Watts) (12/01/89)
In article <303@usna.MIL>, baldwin@usna.MIL (J.D. Baldwin) writes: > Is there a good, clean way to convert the value of an enumerated ordinal > type to a string value? > > That is, what does the function ConverterFunction(somevalue:sometype) : string > look like in this fragment: > > type veggies = (carrots, limabeans, beets, toadstools); . . . > var whatveg : veggies; . . . > > whatveg := carrots; > . . . > writeln(ConverterFunction(whatveg)); > > so that writeln statement will output the string "carrots"? This question > is not exactly mission-critical to me, but an answer would be nice. If > replies are by e-mail, I'll summarize. Unfortunately, there's no easy way to do this, as the names of the values are not stored in the output file! However, in Turbo Pascal, you could do the following: type string10 = string[10]; veggies = (carrots, limabeans, beets, toadstools); const veggies_string : array[0..3] of string10 = ('carrots', 'limabeans', 'beets', 'toadstools'); var whatveg : veggies; function ConverterFunction(ToConvert : veggies) : string10; begin ConverterFunction := veggies_string[Ord(ToConvert)]; end; begin whatveg := carrots; writeln(ConverterFunction(whatveg)); end. Unfortunately, you can only use typed constants (the const above) in Turbo Pascal (as far as I know). With standard pascal, you would have to make the array a standard var, and initialize it in the program body. - Jonathan Watts jrwsnsr@jupiter.nmt.edu
reino@cs.eur.nl (Reino de Boer) (12/01/89)
baldwin@usna.MIL (J.D. Baldwin) writes: >Is there a good, clean way to convert the value of an enumerated ordinal >type to a string value? As far as I know, Turbo Pascal doesn't help you on this one. You have to write a function yourself (for each enumeration) like (e.g.) this: case v of carrots : s := 'carrots'; . . . end; stringvalue := s There are some pascals which do the conversion for you (I know VAX-Pascal does it). If there is general Turbo Pascal solution I'd like to hear about it. Hope this helps -- Reino -- Reino R. A. de Boer Erasmus University Rotterdam ( Informatica ) e-mail: reino@cs.eur.nl
mattl@ritcsh.cs.rit.edu (FaceMan) (12/01/89)
In article <303@usna.MIL>, baldwin@usna.MIL (J.D. Baldwin) writes: > Is there a good, clean way to convert the value of an enumerated ordinal > type to a string value? > > That is, what does the function ConverterFunction(somevalue:sometype) : string > look like in this fragment: [ text deleted ] > writeln(ConverterFunction(whatveg)); > > so that the writeln statement will output the string "carrots"? This question One way you could do it is to simply write a function with a CASE statement that assigns the correct value to the function and returns. However, there is an (I think, anyways) easier and more efficient way to do this. What you do is use typed constants to define your values like this: TYPE Days = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); DayStr = String[4]; CONST Day : ARRAY[Days] OF DayStr = ('Sun','Mon','Tues','Wed','Thurs','Fri','Sat'); - or - Day : ARRAY[Sunday..Saturday] < Rest of line looks identical > Now, if you want the string value of a day, simply use: WriteLn(Day[Sunday]); -> Would write out "Sun".... Of course, you would have to write one for each type you define, but it is probably the only way. Once the program is compiled, it really knows nothing about the names the types uses, only their ordinal values. One small sidenote: These constants can be used as pre-defined variables. In other words, they start out with their initial values, but you could, for example, change the "Wed" to "Mid" in the middle of your program. -- You can lead a horse to water. | Matthew | mattl@ritcsh.cs.rit.edu But if you can get him to | "FaceMan" | mal6315@ultb.isc.rit.edu lie down on his back and float, | Lecher | mal6315@ritvax you've really got something there! |=================|=========================
winfave@dutrun.UUCP (Alexander Verbraeck) (12/04/89)
In article <303@usna.MIL> baldwin@cad.usna.mil (J.D. Baldwin) writes: >Is there a good, clean way to convert the value of an enumerated ordinal >type to a string value? > >That is, what does the function ConverterFunction(somevalue:sometype) : string >look like in this fragment: > > type veggies = (carrots, limabeans, beets, toadstools); . . . > var whatveg : veggies; . . . > > whatveg := carrots; > . . . > writeln(ConverterFunction(whatveg)); > >so that the writeln statement will output the string "carrots"? This question >is not exactly mission-critical to me, but an answer would be nice. If >replies are by e-mail, I'll summarize. The way I usually solve this is as follows (Turbo Pascal 3, 4, 5, ...): program vegetables(input,output); type VegType = (carrot, apple); const VegString : array[VegType] of string[10] = ('carrot','apple'); var V : VegType; begin V:=carrot; writeln(VegString[V]); readln; end. I think it is a neat way to do it: using strings of the defined type array[VegType]. In this way you don't need any conversion function, case statement or whatever. ---------------------------------------------------------------------- Alexander Verbraeck e-mail: winfave@dutrun.tudelft.nl Delft University of Technology winfave@hdetud1.bitnet Department of Information Systems winfave@dutrun.uucp PO Box 356, 2600 AJ The Netherlands dutrun!winfave@hp4nl.uucp ----------------------------------------------------------------------
bob@verdix.com (Bob Boulanger) (12/06/89)
In article <303@usna.MIL> baldwin@cad.usna.mil (J.D. Baldwin) writes: >Is there a good, clean way to convert the value of an enumerated ordinal >type to a string value? > You could use an array, like this: CONST Veggie = ARRAY[x..y] OF String[z] ('carrot', 'brocoli', etc); VAR Veg_Name : String[x]; BEGIN Veg_Name := Veggie[x]; Writeln (Veg_Name); END. Obviously, you could leave out the string variable assignment and just say Writeln (Veggie[x]); Hope this helps. Bob Bob Boulanger bob@verdix.com Verdix Corporation 1600 NW Compton Drive
grimesg@annapurna (George Grimes) (12/07/89)
>In article <303@usna.MIL> baldwin@cad.usna.mil (J.D. Baldwin) writes: >Is there a good, clean way to convert the value of an enumerated ordinal >type to a string value? > >That is, what does the function ConverterFunction(somevalue:sometype) : string >look like in this fragment: > > type veggies = (carrots, limabeans, beets, toadstools); . . . > var whatveg : veggies; . . . > > whatveg := carrots; > . . . > writeln(ConverterFunction(whatveg)); > >so that the writeln statement will output the string "carrots"? This question >is not exactly mission-critical to me, but an answer would be nice. If >replies are by e-mail, I'll summarize. On the VAX you can just do writeln(whatveg); and VAX Pascal will take care of the conversion to a string automatically. Likewise, if you have readln(whatveg); your user can enter "carrot" and conversion to the proper ordinal is automatic. When I first bought Microsoft Pascal in 1982, the manual showed an example like this. After I bought it and got it home I found the obscure reference in an appendix that said that this feature was not implemented yet. When I called and asked about it, they said that this feature would be available very soon. The last time I called and asked about this (about the fourth call I think) a year or two ago, I was still assured that this would happen 'real soon now'. It's a nice feature and I'd love to have it on my PC. George ******************************************************************************* Any similarity between my opinion and my employer's is purely coincidental and subject to immediate review if you bring it to my attention! +-------------------------------------------------------------------+ | DOMAIN: grimesg@sj.ate.slb.com | George Grimes | | UUCP: {decwrl,uunet}!sjsca4!grimesg | Schlumberger Technologies | | INTERNET: grimesg%sjs@sdr.slb.com | 1601 Technology Drive | | PHONE:(408)437-5305/Fax:(408)453-0137 | San Jose, Ca. 95115 | +-------------------------------------------------------------------+