From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,e2413c4fccb33dbc X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!news.glorb.com!wn13feed!worldnet.att.net!bgtnsc04-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada From: anon@anon.org (anon) Subject: Re: determining input data type Reply-To: no to spamers (No@email.given.org) References: X-Newsreader: IBM NewsReader/2 2.0 Message-ID: Date: Sat, 18 Oct 2008 18:54:10 GMT NNTP-Posting-Host: 12.64.6.17 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1224356050 12.64.6.17 (Sat, 18 Oct 2008 18:54:10 GMT) NNTP-Posting-Date: Sat, 18 Oct 2008 18:54:10 GMT Organization: AT&T Worldnet Xref: g2news1.google.com comp.lang.ada:2441 Date: 2008-10-18T18:54:10+00:00 List-Id: -- -- Without knowing what format your data is in it is kind of -- hard to decide what way is the best. Some ways are easy -- while others are more complex -- -- But if the data is a packed buffer string then you could use -- Ada.Characters.Handling.Is_Digit. -- -- In this case the best was is to use a loop and search for all -- valid data where the search routine would find the integer -- value and no error occurs unless the data is float. Also in this -- example, only base 10 integers are valid. -- with Ada.Text_IO ; use Ada.Text_IO ; with Ada.Integer_Text_IO ; use Ada.Integer_Text_IO ; with Ada.Characters.Handling ; use Ada.Characters.Handling ; procedure b is Data_Error : exception ; function To_Integer ( Buffer : String ) return Integer is Index : Natural ; First : Natural ; -- first valid digit Last : Natural ; -- Last valid digit begin -- To_Integer Index := Buffer'First ; while Index <= Buffer'Last and then not is_digit ( Buffer ( Index ) ) loop Index := Index + 1 ; end loop ; First := Index ; while Index <= Buffer'Last and then is_digit ( Buffer ( Index ) ) loop Index := Index + 1 ; end loop ; Last := Index - 1 ; -- check for decimal point aka the number is float if Index <= Buffer'Last and then Buffer ( Index ) = '.' then raise Data_Error ; end if ; return Integer'Value ( Buffer ( First..Last ) ) ; exception when Data_Error => Put_Line ( "Data was invalid -- need to check data source" ) ; Put_Line ( "Invalid Data element =>" & Buffer ( Index ) ) ; raise ; end To_Integer ; Data : integer ; begin -- b Data := To_Integer ( " =123;" ) ; Put ( "Data => " ) ; Put ( Data ) ; New_Line ; -- Data_Error is raised Data := To_Integer ( "asv456" ) ; Put ( "Data => " ) ; Put ( Data ) ; New_Line ; -- Data_Error is raised Data := To_Integer ( " =789.;" ) ; Put ( "Data => " ) ; Put ( Data ) ; New_Line ; exception when Data_Error => null ; end b ; In , jedivaughn writes: >Hi, How is the best way to determine the type of input. I have the >input coming in through a string but if the contents of the string is >an integer then I want to convert it to such. This seems like it >should be very easy to do but I can't seem to find out how to do it >any where. > > >Thanks, > >John