comp.lang.ada
 help / color / mirror / Atom feed
* Beginner Question
@ 2010-10-20 21:41 SpoonThief
  2010-10-20 22:43 ` Jeffrey Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: SpoonThief @ 2010-10-20 21:41 UTC (permalink / raw)


So, I have a question that may seem painfully obvious, but how does
one go about parsing a Float from a string that has other data in it
too? I'm getting a line from the keyboard that has a float and a
custom type in it, and I need to separate the two values. I'm using a
call to Get_Line to get the entire line the user entered as a string,
and when I try to call Float'Value (In_String (1 .. Last)), I get a
constraint error. Is there any way to parse a float and a custom
subtype (Excellent, Good, Fair, Poor) from one string, or am I going
about this the entirely wrong way? Any help would be much appreciated,
and if clarification is needed because I wasn't clear, just ask. Thank
you in advance!



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

* Re: Beginner Question
  2010-10-20 21:41 Beginner Question SpoonThief
@ 2010-10-20 22:43 ` Jeffrey Carter
  2010-10-20 22:45 ` Adam Beneschan
  2010-10-21  7:28 ` Dmitry A. Kazakov
  2 siblings, 0 replies; 4+ messages in thread
From: Jeffrey Carter @ 2010-10-20 22:43 UTC (permalink / raw)


On 10/20/2010 02:41 PM, SpoonThief wrote:
> So, I have a question that may seem painfully obvious, but how does
> one go about parsing a Float from a string that has other data in it
> too? I'm getting a line from the keyboard that has a float and a
> custom type in it, and I need to separate the two values. I'm using a
> call to Get_Line to get the entire line the user entered as a string,
> and when I try to call Float'Value (In_String (1 .. Last)), I get a
> constraint error. Is there any way to parse a float and a custom
> subtype (Excellent, Good, Fair, Poor) from one string, or am I going
> about this the entirely wrong way? Any help would be much appreciated,
> and if clarification is needed because I wasn't clear, just ask. Thank
> you in advance!

You might want to look at Ada.Text_IO.Float_IO, specifically

procedure Get(From : in String; Item : out Num; Last : out Positive);

(ARM A.10.1; http://www.adaic.org/standards/05rm/html/RM-A-10-1.html).

-- 
Jeff Carter
"You empty-headed animal-food-trough wiper."
Monty Python & the Holy Grail
04



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

* Re: Beginner Question
  2010-10-20 21:41 Beginner Question SpoonThief
  2010-10-20 22:43 ` Jeffrey Carter
@ 2010-10-20 22:45 ` Adam Beneschan
  2010-10-21  7:28 ` Dmitry A. Kazakov
  2 siblings, 0 replies; 4+ messages in thread
From: Adam Beneschan @ 2010-10-20 22:45 UTC (permalink / raw)


On Oct 20, 2:41 pm, SpoonThief <spoonth...@gmail.com> wrote:
> So, I have a question that may seem painfully obvious, but how does
> one go about parsing a Float from a string that has other data in it
> too? I'm getting a line from the keyboard that has a float and a
> custom type in it, and I need to separate the two values. I'm using a
> call to Get_Line to get the entire line the user entered as a string,
> and when I try to call Float'Value (In_String (1 .. Last)), I get a
> constraint error. Is there any way to parse a float and a custom
> subtype (Excellent, Good, Fair, Poor) from one string, or am I going
> about this the entirely wrong way? Any help would be much appreciated,
> and if clarification is needed because I wasn't clear, just ask. Thank
> you in advance!

Float'Value expects the entire string to look like a float.

The Float_IO generic in Ada.Text_IO has a Get routine that will let
you parse a float off the beginning of the string.  (If you're using
the predefined Float type, Ada.Float_Text_IO is a ready-made
instantiation of Float_IO.)  Similarly, there's an Enumeration_IO
generic in Ada.Text_IO that will let you parse your custom enumeration
type.  Both routines return a Last parameter that gives you the index
of the last character it used.  So something like this should work, if
T is your enumeration type:

   The_Float : Float;
   The_T : T;
   F_Last : Positive;
   T_Last : Positive;

   package T_IO is new Ada.Text_IO.Enumeration_IO (T);

   Ada.Float_Text_IO.Get (In_String (1 .. Last), The_Float, F_Last);
   T_IO.Get (In_String (F_Last + 1 .. Last), The_T, T_Last);

More info is in RM sections A.10.9 and A.10.10.

Those are simple ways to get things to work.  If you need more control
over the parsing, check out the operations in Ada.Strings.Fixed (RM A.
4.3); you could use those to search for blanks or whatever other
character you need to, and then once you figure out the beginning and
ending indexes of the floating-point part of the input, say, then you
can use Float'Value (In_String (Begin_Index .. End_Index)).

                             -- Adam



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

* Re: Beginner Question
  2010-10-20 21:41 Beginner Question SpoonThief
  2010-10-20 22:43 ` Jeffrey Carter
  2010-10-20 22:45 ` Adam Beneschan
@ 2010-10-21  7:28 ` Dmitry A. Kazakov
  2 siblings, 0 replies; 4+ messages in thread
From: Dmitry A. Kazakov @ 2010-10-21  7:28 UTC (permalink / raw)


On Wed, 20 Oct 2010 14:41:08 -0700 (PDT), SpoonThief wrote:

> So, I have a question that may seem painfully obvious, but how does
> one go about parsing a Float from a string that has other data in it
> too? I'm getting a line from the keyboard that has a float and a
> custom type in it, and I need to separate the two values.

I have a set of libraries for parsing strings. This one is for integer and
floating point numbers:

http://www.dmitry-kazakov.de/ada/strings_edit.htm

> I'm using a
> call to Get_Line to get the entire line the user entered as a string,

You will call Get, which takes a float number at the current string
position and then advances the position pointer behind the number.

[ For dimensioned floating point number parsing see:
http://www.dmitry-kazakov.de/ada/units.htm ]

> and when I try to call Float'Value (In_String (1 .. Last)), I get a
> constraint error. Is there any way to parse a float and a custom
> subtype (Excellent, Good, Fair, Poor) from one string, or am I going
> about this the entirely wrong way?

http://www.dmitry-kazakov.de/ada/tables.htm

It pursues the same principle. You have a table of tokens (mapping of texts
to some type). The table has the operation Get, which matches the string at
the current position, finds the longest match, returns the corresponding
value and advances the position pointer.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

end of thread, other threads:[~2010-10-21  7:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-20 21:41 Beginner Question SpoonThief
2010-10-20 22:43 ` Jeffrey Carter
2010-10-20 22:45 ` Adam Beneschan
2010-10-21  7:28 ` Dmitry A. Kazakov

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