comp.lang.ada
 help / color / mirror / Atom feed
* Converting Char to Enum
@ 2002-03-02 18:17 Lars Noschinski
  2002-03-03  1:15 ` sk
                   ` (3 more replies)
  0 siblings, 4 replies; 17+ messages in thread
From: Lars Noschinski @ 2002-03-02 18:17 UTC (permalink / raw)


Hello!

In my Ada95 program, I have a enumeration type defined as follows:

type symbols is ('0', '1', 'B', 'T', 'U');

The program takes a string from the command-line and needs to convert the 
chars to symbols. At the moment, I use a case-construct, but I wonder if 
there is a more elegant possibility.


CU Lars Noschinski



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

* Re: Converting Char to Enum
  2002-03-02 18:17 Lars Noschinski
@ 2002-03-03  1:15 ` sk
  2002-03-03 16:43   ` Robert Dewar
  2002-03-03  6:30 ` tmoran
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 17+ messages in thread
From: sk @ 2002-03-03  1:15 UTC (permalink / raw)


Hi,

Try using the 'Value attribute of "Symbols" type to get
a value of "Symbols" type from a string.

Try using the 'Image attribute of "Symbols" type to get
a string representation of a specific "Symbols" entity.

-------------------------------------
-- Merge vertically for real address
-------------------------------------
s n p @ t . o
 k i e k c c m
-------------------------------------



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

* Re: Converting Char to Enum
  2002-03-02 18:17 Lars Noschinski
  2002-03-03  1:15 ` sk
@ 2002-03-03  6:30 ` tmoran
  2002-03-04 11:01 ` sk
       [not found] ` <3C835426.DA864199@myob.com>
  3 siblings, 0 replies; 17+ messages in thread
From: tmoran @ 2002-03-03  6:30 UTC (permalink / raw)


>The program takes a string from the command-line and needs to convert the
>chars to symbols. At the moment, I use a case-construct, but I wonder if
>there is a more elegant possibility.
  Convert : constant array(Characters) of Enum := ...



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

* Re: Converting Char to Enum
  2002-03-03  1:15 ` sk
@ 2002-03-03 16:43   ` Robert Dewar
  2002-03-03 18:54     ` Lars Noschinski
  0 siblings, 1 reply; 17+ messages in thread
From: Robert Dewar @ 2002-03-03 16:43 UTC (permalink / raw)


sk <noname@myob.com> wrote in message news:<mailman.1015118162.1026.comp.lang.ada@ada.eu.org>...
> Hi,
> 
> Try using the 'Value attribute of "Symbols" type to get
> a value of "Symbols" type from a string.
> 
> Try using the 'Image attribute of "Symbols" type to get
> a string representation of a specific "Symbols" entity.

Ouch! From heavy to heavier!
An array is clearly the obvious efficient solution here.



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

* Re: Converting Char to Enum
  2002-03-03 16:43   ` Robert Dewar
@ 2002-03-03 18:54     ` Lars Noschinski
  2002-03-03 20:07       ` Larry Kilgallen
  2002-03-03 20:13       ` tmoran
  0 siblings, 2 replies; 17+ messages in thread
From: Lars Noschinski @ 2002-03-03 18:54 UTC (permalink / raw)


dewar@gnat.com (Robert Dewar) wrote:
> Ouch! From heavy to heavier!
> An array is clearly the obvious efficient solution here.

You mean something like

  convert : constant array(characters) of symbols := ...

  sym : symbols;
  char : characters;

and an assignment like

"sym := convert(char)"?

In this case, how I catch invalid characters?


CU Lars Noschinski



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

* Re: Converting Char to Enum
  2002-03-03 18:54     ` Lars Noschinski
@ 2002-03-03 20:07       ` Larry Kilgallen
  2002-03-03 20:21         ` Lars Noschinski
  2002-03-03 20:13       ` tmoran
  1 sibling, 1 reply; 17+ messages in thread
From: Larry Kilgallen @ 2002-03-03 20:07 UTC (permalink / raw)


In article <Xns91C6CA7E41AB5Lars17NoCBW@larsno.fqdn.th-h.de>, Lars Noschinski <lars@usenet.noschinski.de> writes:
> dewar@gnat.com (Robert Dewar) wrote:
>> Ouch! From heavy to heavier!
>> An array is clearly the obvious efficient solution here.
> 
> You mean something like
> 
>   convert : constant array(characters) of symbols := ...
> 
>   sym : symbols;
>   char : characters;
> 
> and an assignment like
> 
> "sym := convert(char)"?
> 
> In this case, how I catch invalid characters?

Perhaps with one additional symbol named INVALID_CHARACTER.



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

* Re: Converting Char to Enum
  2002-03-03 18:54     ` Lars Noschinski
  2002-03-03 20:07       ` Larry Kilgallen
@ 2002-03-03 20:13       ` tmoran
  1 sibling, 0 replies; 17+ messages in thread
From: tmoran @ 2002-03-03 20:13 UTC (permalink / raw)


>  convert : constant array(characters) of symbols := ...
> ...
>"sym := convert(char)"?
> ...
>In this case, how I catch invalid characters?
  OK : constant array(Character) of Boolean
    := ('0' | '1' | 'B' | 'T' | 'U' => True, others => False);
  if OK(char) then sym := convert(char);else ...
Note that this is an example of trading away memory to gain speed and,
in this case, lose complexity.  You should always consider the possibility
and desirability of such a tradeoff whenever you decide how to code something.



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

* Re: Converting Char to Enum
  2002-03-03 20:07       ` Larry Kilgallen
@ 2002-03-03 20:21         ` Lars Noschinski
  2002-03-03 22:46           ` Larry Kilgallen
  2002-03-04  6:55           ` Jeffrey Carter
  0 siblings, 2 replies; 17+ messages in thread
From: Lars Noschinski @ 2002-03-03 20:21 UTC (permalink / raw)


Kilgallen@SpamCop.net (Larry Kilgallen) wrote:
> Lars Noschinski <lars@usenet.noschinski.de> writes: 
>>   convert : constant array(characters) of symbols := ...
[...]
>> "sym := convert(char)"?
>>
>> In this case, how I catch invalid characters?
>
> Perhaps with one additional symbol named INVALID_CHARACTER.

Ok, that's a possibility. But I thougt, inband-signalling would be a no-
no?


CU Lars Noschinski



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

* Re: Converting Char to Enum
  2002-03-03 20:21         ` Lars Noschinski
@ 2002-03-03 22:46           ` Larry Kilgallen
  2002-03-04  6:55           ` Jeffrey Carter
  1 sibling, 0 replies; 17+ messages in thread
From: Larry Kilgallen @ 2002-03-03 22:46 UTC (permalink / raw)


In article <Xns91C6D92FB9742Lars17NoCBW@larsno.fqdn.th-h.de>, Lars Noschinski <lars@usenet.noschinski.de> writes:
> Kilgallen@SpamCop.net (Larry Kilgallen) wrote:
>> Lars Noschinski <lars@usenet.noschinski.de> writes: 
>>>   convert : constant array(characters) of symbols := ...
> [...]
>>> "sym := convert(char)"?
>>>
>>> In this case, how I catch invalid characters?
>>
>> Perhaps with one additional symbol named INVALID_CHARACTER.
> 
> Ok, that's a possibility. But I thougt, inband-signalling would be a no-
> no?

It depends on whose rules you are using, and how well you localize
that other symbol.  If you do conversion in a function, that could
be (by your own decision) the only place where INVALID_CHARACTER is
used.

Case statements elsewhere could have:

	when others =>
		-- something is desparately wrong...



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

* Re: Converting Char to Enum
  2002-03-03 20:21         ` Lars Noschinski
  2002-03-03 22:46           ` Larry Kilgallen
@ 2002-03-04  6:55           ` Jeffrey Carter
  1 sibling, 0 replies; 17+ messages in thread
From: Jeffrey Carter @ 2002-03-04  6:55 UTC (permalink / raw)


Lars Noschinski wrote:
> 
> Kilgallen@SpamCop.net (Larry Kilgallen) wrote:
> > Lars Noschinski <lars@usenet.noschinski.de> writes:
> >>   convert : constant array(characters) of symbols := ...
> [...]
> >> "sym := convert(char)"?
> >>
> >> In this case, how I catch invalid characters?
> >
> > Perhaps with one additional symbol named INVALID_CHARACTER.
> 
> Ok, that's a possibility. But I thougt, inband-signalling would be a no-
> no?

If it makes your code more complex, then avoiding inband signaling is
probably the wrong approach.

In real life, you would use the extra enumeration value only in the part
of the code that interfaces with the outside world. The rest of the
software would be restricted to a subtype (Valid_Symbol?) of the
enumeration type that excludes this extra value.

-- 
Jeff Carter
"Nobody expects the Spanish Inquisition!"
Monty Python's Flying Circus



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

* Re: Converting Char to Enum
  2002-03-02 18:17 Lars Noschinski
  2002-03-03  1:15 ` sk
  2002-03-03  6:30 ` tmoran
@ 2002-03-04 11:01 ` sk
       [not found] ` <3C835426.DA864199@myob.com>
  3 siblings, 0 replies; 17+ messages in thread
From: sk @ 2002-03-04 11:01 UTC (permalink / raw)


Hi,

Original Post:

Lars Noschinski <lars@usenet.noschinski.de> :
>...
>there is a more elegant possibility.
>                ^^^^^^^

<my suggestion snipped>

dewar@gnat.com (Robert Dewar) :
>Ouch! From heavy to heavier!
>An array is clearly the obvious efficient solution here.
>                                ^^^^^^^^^

I find 'Image, 'Value and kin elegant. I will
concede efficiency :-)

Word choice aside, are the attribute functions 
inherently (?sp) inefficient ? Or is the example 
of the OP a specialized and reduced case ?

type Printer_Status is (Available, Busy, Error, ...);

I think I would prefer to rely on the strategy
outlined by the comments in "System.Val_Enum" 
(spec, GNAT) than developing my own parser for
every enumeration type :-).

-------------------------------------
-- Merge vertically for real address
-------------------------------------
s n p @ t . o
 k i e k c c m
-------------------------------------



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

* Re: Converting Char to Enum
       [not found] ` <3C835426.DA864199@myob.com>
@ 2002-03-04 12:48   ` David C. Hoos, Sr.
  2002-03-04 16:36     ` Lars Noschinski
  0 siblings, 1 reply; 17+ messages in thread
From: David C. Hoos, Sr. @ 2002-03-04 12:48 UTC (permalink / raw)


I think the reason that V'alue and 'Image  are not appropriate
for the original poster's case is that his case was a subset of
the type Character.

For enumeration types in general, the use of 'Value and 'Image
are indeed.appropriate.


----- Original Message ----- 
From: "sk" <noname@myob.com>
To: <comp.lang.ada@ada.eu.org>
Sent: March 04, 2002 5:01 AM
Subject: Re: Converting Char to Enum


> Hi,
> 
> Original Post:
> 
> Lars Noschinski <lars@usenet.noschinski.de> :
> >...
> >there is a more elegant possibility.
> >                ^^^^^^^
> 
> <my suggestion snipped>
> 
> dewar@gnat.com (Robert Dewar) :
> >Ouch! From heavy to heavier!
> >An array is clearly the obvious efficient solution here.
> >                                ^^^^^^^^^
> 
> I find 'Image, 'Value and kin elegant. I will
> concede efficiency :-)
> 
> Word choice aside, are the attribute functions 
> inherently (?sp) inefficient ? Or is the example 
> of the OP a specialized and reduced case ?
> 
> type Printer_Status is (Available, Busy, Error, ...);
> 
> I think I would prefer to rely on the strategy
> outlined by the comments in "System.Val_Enum" 
> (spec, GNAT) than developing my own parser for
> every enumeration type :-).
> 
> -------------------------------------
> -- Merge vertically for real address
> -------------------------------------
> s n p @ t . o
>  k i e k c c m
> -------------------------------------
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 





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

* Re: Converting Char to Enum
  2002-03-04 12:48   ` David C. Hoos, Sr.
@ 2002-03-04 16:36     ` Lars Noschinski
  2002-03-04 23:17       ` David C. Hoos
  0 siblings, 1 reply; 17+ messages in thread
From: Lars Noschinski @ 2002-03-04 16:36 UTC (permalink / raw)


"David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> wrote:
> I think the reason that V'alue and 'Image  are not appropriate
> for the original poster's case is that his case was a subset of
> the type Character.

| type Symbols is ('0', '1', 'B', 'T', 'U');

Is that really a subset of character? I'm asking, because the following 
code prints "'0'", "'B'" etc. and not "0", "B".

--------------------------------------------------------------------
procedure Put_Symbol(sym : symbol) is

    	package TIO is new Ada.Text_Io.Enumeration_Io(Symbols);

begin -- Put_Symbol
  TIO.Put(Sym);
end Put_Symbol.
--------------------------------------------------------------------


CU Lars Noschinski



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

* Re: Converting Char to Enum
  2002-03-04 16:36     ` Lars Noschinski
@ 2002-03-04 23:17       ` David C. Hoos
  2002-03-05 15:55         ` Lars Noschinski
  0 siblings, 1 reply; 17+ messages in thread
From: David C. Hoos @ 2002-03-04 23:17 UTC (permalink / raw)


----- Original Message ----- 
From: "Lars Noschinski" <lars@usenet.noschinski.de>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Monday, March 04, 2002 10:36 AM
Subject: Re: Converting Char to Enum


> "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> wrote:
> > I think the reason that V'alue and 'Image  are not appropriate
> > for the original poster's case is that his case was a subset of
> > the type Character.
> 
> | type Symbols is ('0', '1', 'B', 'T', 'U');
> 
> Is that really a subset of character? I'm asking, because the following 
> code prints "'0'", "'B'" etc. and not "0", "B".
> 
Take a look at two things, viz.:

   1.  The definition of the Character type in LRM A.1 (35)

   2.   The output of this program:

with Ada.Text_IO;
procedure Lars
is
   type Symbols is ('0', '1', 'B', 'T', 'U');

   package Symbol_IO is new Ada.Text_IO.Enumeration_IO (Symbols);

   package Character_IO is new Ada.Text_IO.Enumeration_IO (Character);

begin
   Ada.Text_IO.Put_Line ("Symbols:");
   for S in Symbols loop
      Symbol_IO.Put (S);
      Ada.Text_IO.Put (", ");
   end loop;
   Ada.Text_IO.New_Line (2);
   Ada.Text_IO.Put_Line ("Characters:");
   for C in Character loop
      Character_IO.Put (C);
      Ada.Text_IO.Put (", ");
   end loop;
   Ada.Text_IO.New_Line (2);
end Lars;
> --------------------------------------------------------------------
> procedure Put_Symbol(sym : symbol) is
> 
>     package TIO is new Ada.Text_Io.Enumeration_Io(Symbols);
> 
> begin -- Put_Symbol
>   TIO.Put(Sym);
> end Put_Symbol.
> --------------------------------------------------------------------
> 
> 
> CU Lars Noschinski
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 




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

* Re: Converting Char to Enum
  2002-03-04 23:17       ` David C. Hoos
@ 2002-03-05 15:55         ` Lars Noschinski
  2002-03-05 20:15           ` David C. Hoos
  0 siblings, 1 reply; 17+ messages in thread
From: Lars Noschinski @ 2002-03-05 15:55 UTC (permalink / raw)


"David C. Hoos" <david.c.hoos.sr@ada95.com> wrote:
> From: "Lars Noschinski" <lars@usenet.noschinski.de>
>> "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> wrote:
>> | type Symbols is ('0', '1', 'B', 'T', 'U');
>>
>> Is that really a subset of character? I'm asking, because the
>> following code prints "'0'", "'B'" etc. and not "0", "B".
>>
> Take a look at two things, viz.:
>
>    1.  The definition of the Character type in LRM A.1 (35)

Ok, I see. Character is also an enumeration.
 
>    2.   The output of this program:

So if I want to output only the character without the quotes, I have
to convert it to string? In the case of type symbols with an
indirection through type character, of course. 

BTW: Which online book/tutorial has a good explanation of the
possibilities of type conversion in Ada? After doing PHP for a few
weeks, I really see the advantages of a strong type system, but it
needs some time to get familiar with it. 


CU Lars Noschinski



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

* Re: Converting Char to Enum
  2002-03-05 15:55         ` Lars Noschinski
@ 2002-03-05 20:15           ` David C. Hoos
  0 siblings, 0 replies; 17+ messages in thread
From: David C. Hoos @ 2002-03-05 20:15 UTC (permalink / raw)



----- Original Message ----- 
From: "Lars Noschinski" <lars@usenet.noschinski.de>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Tuesday, March 05, 2002 9:55 AM
Subject: Re: Converting Char to Enum
<snip>
> So if I want to output only the character without the quotes, I have
> to convert it to string? In the case of type symbols with an
> indirection through type character, of course. 
No, characters can be output with Text_IO.Put (e.g.), instead of
an instantiation of Text_IO.Enumeration_IO, in which case just
the character will be output.

> 
> BTW: Which online book/tutorial has a good explanation of the
> possibilities of type conversion in Ada? After doing PHP for a few
> weeks, I really see the advantages of a strong type system, but it
> needs some time to get familiar with it. 
> 
> 
> CU Lars Noschinski
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 




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

* Re: Converting Char to Enum
@ 2002-03-06  6:23 Christoph Grein
  0 siblings, 0 replies; 17+ messages in thread
From: Christoph Grein @ 2002-03-06  6:23 UTC (permalink / raw)


> So if I want to output only the character without the quotes, I have
> to convert it to string?

No, you have to use the character operations of Text_IO (if your characters are 
of type Standard.Character. No conversion to Standard.String is necessary.

Note that you may write:
ote that you may write:

procedure My_Program is

  type My_Character is (something, 'A', 'a', 'B', 'b', anything);
  type My_String is array (Positive range <>) of My_Character;

  V: My_String := "AAbbbA" & something & "aBBB";

begin

  ...

end My_Program;

String literals like "AAbbbA" are available for all character types, a character 
type being a type with at least one character literal, a character literal being 
something like 'a'. Note that the apperance of something and anything above in 
the type definition does not prevent My_Character from being a character type, 
you only cannot put these literals into a string literal, you have to do it in 
the way above. These strings are however not compatible with Standard.String, so 
you cannot use Text_IO with them directly.



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

end of thread, other threads:[~2002-03-06  6:23 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-06  6:23 Converting Char to Enum Christoph Grein
  -- strict thread matches above, loose matches on Subject: below --
2002-03-02 18:17 Lars Noschinski
2002-03-03  1:15 ` sk
2002-03-03 16:43   ` Robert Dewar
2002-03-03 18:54     ` Lars Noschinski
2002-03-03 20:07       ` Larry Kilgallen
2002-03-03 20:21         ` Lars Noschinski
2002-03-03 22:46           ` Larry Kilgallen
2002-03-04  6:55           ` Jeffrey Carter
2002-03-03 20:13       ` tmoran
2002-03-03  6:30 ` tmoran
2002-03-04 11:01 ` sk
     [not found] ` <3C835426.DA864199@myob.com>
2002-03-04 12:48   ` David C. Hoos, Sr.
2002-03-04 16:36     ` Lars Noschinski
2002-03-04 23:17       ` David C. Hoos
2002-03-05 15:55         ` Lars Noschinski
2002-03-05 20:15           ` David C. Hoos

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