comp.lang.ada
 help / color / mirror / Atom feed
* how to check if a string variable contains a number or string?
@ 2002-11-18 20:41 Sarah Thomas
  2002-11-18 21:09 ` Stephen Leake
                   ` (4 more replies)
  0 siblings, 5 replies; 12+ messages in thread
From: Sarah Thomas @ 2002-11-18 20:41 UTC (permalink / raw)


I have an array of strings that I read in from a file. I want to check
if each element in the array is a string or if it is a number. is
there any predefined functions that would help do that? thanks



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

* Re: how to check if a string variable contains a number or string?
  2002-11-18 20:41 how to check if a string variable contains a number or string? Sarah Thomas
@ 2002-11-18 21:09 ` Stephen Leake
  2002-11-18 21:50   ` David C. Hoos
  2002-11-19  0:44 ` sk
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 12+ messages in thread
From: Stephen Leake @ 2002-11-18 21:09 UTC (permalink / raw)


mabes180@aol.com (Sarah Thomas) writes:

> I have an array of strings that I read in from a file. I want to check
> if each element in the array is a string or if it is a number. is
> there any predefined functions that would help do that? thanks

This sounds like a homework problem. If it is, please say so. We don't
do homework for you, but we can give advice.

Since you are reading strings, obviously what you got is a string.
It's possible that the string contains a valid number; I guess that is
what you want to check.

Try reading the number from the string using Ada.Text_IO.*.

-- 
-- Stephe



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

* Re: how to check if a string variable contains a number or string?
  2002-11-18 21:09 ` Stephen Leake
@ 2002-11-18 21:50   ` David C. Hoos
  0 siblings, 0 replies; 12+ messages in thread
From: David C. Hoos @ 2002-11-18 21:50 UTC (permalink / raw)



----- Original Message ----- 
From: "Stephen Leake" <stephen.a.leake.1@gsfc.nasa.gov>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Monday, November 18, 2002 3:09 PM
Subject: Re: how to check if a string variable contains a number or string?


> mabes180@aol.com (Sarah Thomas) writes:
> 
> > I have an array of strings that I read in from a file. I want to check
> > if each element in the array is a string or if it is a number. is
> > there any predefined functions that would help do that? thanks
> 
> This sounds like a homework problem. If it is, please say so. We don't
> do homework for you, but we can give advice.
> 
> Since you are reading strings, obviously what you got is a string.
> It's possible that the string contains a valid number; I guess that is
> what you want to check.
> 
> Try reading the number from the string using Ada.Text_IO.*
It's cheaper and easier to use, say, Long_Float'Value, since 'Value for
real types will accept strings with or without a decimal point.

The downside is that Ada will disregard underscores embedded in
strings when attempting to convert a string to a numeric value.

If that's OK, then if calling Long_Float'Value doesn't raise
Constraint_Error, then the string is a valid decimal representation of a
number.

> 
> -- 
> -- Stephe
> _______________________________________________
> 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] 12+ messages in thread

* Re: how to check if a string variable contains a number or string?
  2002-11-18 20:41 how to check if a string variable contains a number or string? Sarah Thomas
  2002-11-18 21:09 ` Stephen Leake
@ 2002-11-19  0:44 ` sk
  2002-11-19  1:21   ` Jeffrey Carter
  2002-11-19  1:26 ` Jeffrey Carter
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 12+ messages in thread
From: sk @ 2002-11-19  0:44 UTC (permalink / raw)


Hi, 

1) Stephen Leake <stephen.a.leake.1@gsfc.nasa.gov>
> Try reading the number from the string using Ada.Text_IO.*.


2) "David C. Hoos" <david.c.hoos.sr@ada95.com>
> If that's OK, then if calling Long_Float'Value doesn't 
> raise Constraint_Error, then the string is a valid 
> decimal representation of a number.

OR, 

3) Tokenize the string separating on spaces and use
the Ada.Characters.Handling package ...

   function Is_Control           (Item : in Character) return Boolean;
   function Is_Graphic           (Item : in Character) return Boolean;
   function Is_Letter            (Item : in Character) return Boolean;
   function Is_Lower             (Item : in Character) return Boolean;
   function Is_Upper             (Item : in Character) return Boolean;
   function Is_Basic             (Item : in Character) return Boolean;
   function Is_Digit             (Item : in Character) return Boolean;
   function Is_Decimal_Digit     (Item : in Character) return Boolean
                                                          renames
Is_Digit;
   function Is_Hexadecimal_Digit (Item : in Character) return Boolean;
   function Is_Alphanumeric      (Item : in Character) return Boolean;
   function Is_Special           (Item : in Character) return Boolean;

... yes, time consuming, boring and not "sexy" but better,
IMO, than using exception routines for predictable error
circumstances.

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



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

* Re: how to check if a string variable contains a number or string?
  2002-11-19  0:44 ` sk
@ 2002-11-19  1:21   ` Jeffrey Carter
  2002-11-19  4:41     ` sk
  0 siblings, 1 reply; 12+ messages in thread
From: Jeffrey Carter @ 2002-11-19  1:21 UTC (permalink / raw)


sk wrote:
> 
> ... yes, time consuming, boring and not "sexy" but better,
> IMO, than using exception routines for predictable error
> circumstances.

Reinventing the wheel is never better than reusing existing subprograms.

-- 
Jeff Carter
"Brave Sir Robin ran away."
Monty Python and the Holy Grail




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

* Re: how to check if a string variable contains a number or string?
  2002-11-18 20:41 how to check if a string variable contains a number or string? Sarah Thomas
  2002-11-18 21:09 ` Stephen Leake
  2002-11-19  0:44 ` sk
@ 2002-11-19  1:26 ` Jeffrey Carter
  2002-11-19  3:19 ` SteveD
  2002-11-19  9:38 ` Preben Randhol
  4 siblings, 0 replies; 12+ messages in thread
From: Jeffrey Carter @ 2002-11-19  1:26 UTC (permalink / raw)


Sarah Thomas wrote:
> I have an array of strings that I read in from a file. I want to check
> if each element in the array is a string or if it is a number. is
> there any predefined functions that would help do that? thanks

Since you have an array of strings, obviously each element of the array 
is a string. Presumably you mean you want to check if a string contains 
the image of a number or not. In that case, what do you mean by a 
number? An integer value, or a real value? Are embedded underscores 
acceptable? How about an exponent? Based numbers? Does 
"2#0101_1010.0011#E5" represent the image of a number?

Once you've answered these questions you can decide if 'Value or 
something from Ada.Text_IO is acceptable, or if you're going to have to 
write code to parse the string manually.

-- 
Jeff Carter
"Brave Sir Robin ran away."
Monty Python and the Holy Grail




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

* Re: how to check if a string variable contains a number or string?
  2002-11-18 20:41 how to check if a string variable contains a number or string? Sarah Thomas
                   ` (2 preceding siblings ...)
  2002-11-19  1:26 ` Jeffrey Carter
@ 2002-11-19  3:19 ` SteveD
  2002-11-19  9:38 ` Preben Randhol
  4 siblings, 0 replies; 12+ messages in thread
From: SteveD @ 2002-11-19  3:19 UTC (permalink / raw)


"Sarah Thomas" <mabes180@aol.com> wrote in message
news:a04a773e.0211181241.4e9e13b0@posting.google.com...
> I have an array of strings that I read in from a file. I want to check
> if each element in the array is a string or if it is a number. is
> there any predefined functions that would help do that? thanks

There are some handy string handling functions in Ada.Strings.Fixed.

To do a quick check to see if a string contains an integer number I usually
create a map (Ada.Strings.Maps) and then search for a character that is
not a digit using Index (Ada.Strings.Fixed).  If I find a non-digit
character
it's not a number.

I just noticed that Ada.Strings.Maps defines a "Character_Sequence" as a
subtype string, and there is a Is_Subset function that could be used to do
the
job as well.

Of course this only works if the "numbers" are integers.

I hope this helps,
SteveD





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

* Re: how to check if a string variable contains a number or string?
  2002-11-19  1:21   ` Jeffrey Carter
@ 2002-11-19  4:41     ` sk
  2002-11-19 17:02       ` Jeffrey Carter
  0 siblings, 1 reply; 12+ messages in thread
From: sk @ 2002-11-19  4:41 UTC (permalink / raw)


Hi,

> Reinventing the wheel is never better than reusing 
> existing subprograms.

To some extent I agree, but consider 

1) Known format files ...
-- First-Name Last-Name Age ...
John Doe 42
Jane Doe q
         ^ Exception OK

2) A file of one type only ...
34.7
23.9
143.67
hello <- Exception Ok

3) A random file 
The quick brown Fox bought 3 carrots for $0.34

I would consider using exceptions to realize
that the number and price of carrots are numeric
amounts rather than just strings to be a bad 
technique !

The OP made no mention of fixed format, only
determining strings or numbers.



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



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

* Re: how to check if a string variable contains a number or string?
  2002-11-18 20:41 how to check if a string variable contains a number or string? Sarah Thomas
                   ` (3 preceding siblings ...)
  2002-11-19  3:19 ` SteveD
@ 2002-11-19  9:38 ` Preben Randhol
  4 siblings, 0 replies; 12+ messages in thread
From: Preben Randhol @ 2002-11-19  9:38 UTC (permalink / raw)


Sarah Thomas wrote:
> I have an array of strings that I read in from a file. I want to check
> if each element in the array is a string or if it is a number. is
> there any predefined functions that would help do that? thanks

Hint 1:
   http://www.adapower.com/lang/chartoint.html

Hint 2:
   http://www.it.bton.ac.uk/staff/je/adacraft/ch07.htm

-- 
Preben Randhol ------------------------ http://www.pvv.org/~randhol/ --
�There are three things you can do to a woman. You can love her, suffer
 for her, or turn her into literature.�  - Justine, by Lawrence Durrell



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

* Re: how to check if a string variable contains a number or string?
  2002-11-19  4:41     ` sk
@ 2002-11-19 17:02       ` Jeffrey Carter
  2002-11-24  0:10         ` AG
  0 siblings, 1 reply; 12+ messages in thread
From: Jeffrey Carter @ 2002-11-19 17:02 UTC (permalink / raw)


sk wrote:
> 3) A random file 
> The quick brown Fox bought 3 carrots for $0.34
> 
> I would consider using exceptions to realize
> that the number and price of carrots are numeric
> amounts rather than just strings to be a bad 
> technique !

Given the choice between using existing subprograms that may raise 
exceptions and writing new code to do the same thing but without raising 
an exception, I know of no circumstances in which the latter is an 
acceptable use of resources. I limit myself to professional software 
development; on a personal project you may waste your time 
reimplementing the entire run-time library for all I care.

-- 
Jeff Carter
"It's all right, Taggart. Just a man and a horse being hung out there."
Blazing Saddles




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

* Re: how to check if a string variable contains a number or string?
  2002-11-24  0:10         ` AG
@ 2002-11-23 20:05           ` Jeffrey Carter
  0 siblings, 0 replies; 12+ messages in thread
From: Jeffrey Carter @ 2002-11-23 20:05 UTC (permalink / raw)


AG wrote:
> Tst, tsk... If you use an existing subprogram that may
> raise an exception - don't you have to write some new
> code to handle that exception? And don't you have to
> provide some sort of recovery perhaps to simulate the
> non-exception version?

Perhaps you've discovered some way to use an existing subprogram without 
writing any "new" code?

More to the point, which is a better use of your valuable resources:

with Parser;
...
begin
    Number := Parser.Parse (Image);
    -- process Number
exception
when Parser.Invalid_Image =>
    Error ("Invalid number: " & Image);
end;

or

type Parse_Result (Valid : Boolean := False) is record
    case Valid is
    when False =>
       null;
    when True =>
       Number : Numeric_Value;
    end case;
end record;

function Parse (Image : String) return Parse_Result is
    -- Many
    -- declarations
    -- for
    -- a
    -- complex
    -- parsing
    -- algorithm
    -- identical
    -- to
    -- Parser.Parse
    -- except
    -- for
    -- error
    -- handling
begin -- Parse
    -- a
    -- very
    -- long
    -- and
    -- complex
    -- algorithm
    -- for
    -- parsing
    -- the
    -- character
    -- representation
    -- of
    -- a
    -- number,
    -- identical
    -- to
    -- Parser.Parse
    -- except
    -- for
    -- error
    -- handling
end Parse;
...
Result : Parse_Result;
...
Result := Parse (Image);

if Result.Valid then
    -- process Result.Number
else
    Error ("Invalid number: " & Image);
end if;

?

-- 
Jeff Carter
"I'm a lumberjack and I'm OK."
Monty Python's Flying Circus




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

* Re: how to check if a string variable contains a number or string?
  2002-11-19 17:02       ` Jeffrey Carter
@ 2002-11-24  0:10         ` AG
  2002-11-23 20:05           ` Jeffrey Carter
  0 siblings, 1 reply; 12+ messages in thread
From: AG @ 2002-11-24  0:10 UTC (permalink / raw)


"Jeffrey Carter" <jrcarter@acm.org> wrote in message
news:3DDA6EE2.2010409@acm.org...
> sk wrote:
> > 3) A random file
> > The quick brown Fox bought 3 carrots for $0.34
> >
> > I would consider using exceptions to realize
> > that the number and price of carrots are numeric
> > amounts rather than just strings to be a bad
> > technique !
>
> Given the choice between using existing subprograms that may raise
> exceptions and writing new code to do the same thing but without raising
> an exception, I know of no circumstances in which the latter is an
> acceptable use of resources.

Tst, tsk... If you use an existing subprogram that may
raise an exception - don't you have to write some new
code to handle that exception? And don't you have to
provide some sort of recovery perhaps to simulate the
non-exception version?





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

end of thread, other threads:[~2002-11-24  0:10 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-18 20:41 how to check if a string variable contains a number or string? Sarah Thomas
2002-11-18 21:09 ` Stephen Leake
2002-11-18 21:50   ` David C. Hoos
2002-11-19  0:44 ` sk
2002-11-19  1:21   ` Jeffrey Carter
2002-11-19  4:41     ` sk
2002-11-19 17:02       ` Jeffrey Carter
2002-11-24  0:10         ` AG
2002-11-23 20:05           ` Jeffrey Carter
2002-11-19  1:26 ` Jeffrey Carter
2002-11-19  3:19 ` SteveD
2002-11-19  9:38 ` Preben Randhol

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