Testing if a string is an integer?
[1/13] from: mat::eurogamer::net at: 8-Dec-2000 14:27
Folks,
This is a dead basic one and I'm sure I'll feel stupid when someone
points it out.
Anyway, I have a function which is fed a string. Now I'd like to test
to see if the string is purely numerical.
I guess parse is the right way of doing it and I've seen examples but
something basic like
digits: charset [1234567890]
parse number [digits]
type? and so on appears useless since I KNOW it's a string. I even
tried using error? try[] to trap an error converting to an integer -
no luck with that either.
--
Mat Bettinson - EuroGamer's Gaming Evangelist with a Goatee
http://www.eurogamer.net | http://www.eurogamer-network.com
[2/13] from: philb:upnaway at: 8-Dec-2000 22:46
Hi Matt,
why can you use to-integer
for example
>> a: "1234567890"
== "1234567890"
>> either error? try [to-integer a] [print "Not an integer"] [print
Integer
]
Integer
>> a: "1234567890aaaaaa"
== "1234567890aaaaaa"
>> either error? try [to-integer a] [print "Not an integer"] [print
Integer
]
Not an integer
Cheers Phil
[3/13] from: mat:eurogamer at: 8-Dec-2000 15:18
Heya Philip,
PB> Hi Matt,
PB> why can you use to-integer
Madness. I tried EXACTLY the same thing but I used
number: make integer! number
Thanks anyhow - at least it'll work this way although it's not the
most elegant :)
--
Mat Bettinson - EuroGamer's Gaming Evangelist with a Goatee
http://www.eurogamer.net | http://www.eurogamer-network.com
[4/13] from: emptyhead:home:nl at: 8-Dec-2000 16:20
>> if not error? try [ a: load "66436436" ] [ integer? a ]
== true
>> if not error? try [ a: load "66436fdsaf436" ] [ integer? a ]
== none
>>
Daan Oosterveld
Mat Bettinson schreef:
[5/13] from: carl:cybercraft at: 9-Dec-2000 4:41
On 09-Dec-00, Philip Bevan wrote:
> Hi Matt,
> why can you use to-integer
<<quoted lines omitted: 4>>
> "Integer"]
> Integer
That wouldn't work for decimals in the string as "123.7" would be
converted to an integer and returned as okay.
All I can think of is forall-ing through the string and checking every
character, but I'm sure there's a more simpler way in REBOL.
[6/13] from: g:santilli:tiscalinet:it at: 8-Dec-2000 18:59
Hello Carl!
On 08-Dic-00, you wrote:
CR> All I can think of is forall-ing through the string and
CR> checking every character, but I'm sure there's a more simpler
CR> way in REBOL.
digit: charset "1234567890"
parse string [some digit]
HTH,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/
[7/13] from: carl:cybercraft at: 9-Dec-2000 23:44
On 09-Dec-00, Gabriele Santilli wrote:
> Hello Carl!
> On 08-Dic-00, you wrote:
<<quoted lines omitted: 3>>
> digit: charset "1234567890"
> parse string [some digit]
Ah - very nice. Must look into 'parse properly. Hmmm, why doesn't...
? some
give us some info on 'some?
[8/13] from: dynalt:dynalt at: 9-Dec-2000 3:35
[9/13] from: petr:krenzelok:trz:cz at: 9-Dec-2000 13:59
----- Original Message -----
From: Carl Read <[carl--cybercraft--co--nz]>
To: <[rebol-list--rebol--com]>
Sent: Saturday, December 09, 2000 12:44 PM
Subject: [REBOL] Re: Testing if a string is an integer?
> On 09-Dec-00, Gabriele Santilli wrote:
> > Hello Carl!
<<quoted lines omitted: 7>>
> ? some
> give us some info on 'some?
heh, you should study docs a little bit imo ;-) 'some is part of parse
dialect ...
'some - repeat untill "some digit" is matched
'any - repeat untill "no or more" digits are matched
thru - move "thru" matched string
to - move "to" matched string
'copy str to "something" - copy everything from current position to
something
, put result in 'str
etc.
Cheers,
-pekr-
[10/13] from: al:bri:xtra at: 10-Dec-2000 9:04
Rebol Manual Core.pdf gives full details.
Andrew Martin
ICQ: 26227169 http://members.nbci.com/AndrewMartin/
[11/13] from: g:santilli:tiscalinet:it at: 9-Dec-2000 23:10
Hello Garold!
On 09-Dic-00, you wrote:
GJ> The only answer I have is that 'some is recognized only by
GJ> the parse dialect and not by REBOL: directly, and 'parse is a
GJ> native! word.
That is correct. SOME is a keyword in PARSE's dialect.
GJ> A legal REBOL integer is a bit more complicated (FWIW):
Yup. But I think someone was asking for how to detect if a string
only contains digits. To test if a string is a valid REBOL
integer, you can just do:
integer? try [load string]
Regards,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/
[12/13] from: johnkenyon:ibm at: 11-Dec-2000 10:30
Gabriele and Garold,
Questions like this would be helped if we had a way of adding help to
keywords in a parse dialect. Anyone any ideas of a clean way of doing this?
Thoughts?
Cheers, John
#
Hello Garold!
On 09-Dic-00, you wrote:
GJ> The only answer I have is that 'some is recognized only by
GJ> the parse dialect and not by REBOL: directly, and 'parse is a
GJ> native! word.
That is correct. SOME is a keyword in PARSE's dialect.
GJ> A legal REBOL integer is a bit more complicated (FWIW):
Yup. But I think someone was asking for how to detect if a string
only contains digits. To test if a string is a valid REBOL
integer, you can just do:
integer? try [load string]
Regards,
Gabriele.
[13/13] from: dynalt:dynalt at: 10-Dec-2000 7:58
Gabriele wrote:
> To test if a string is a valid REBOL
integer, you can just do:
integer? try [load string]
That hardly seems fair! I had to work a lot harder than that!
Thanks,
Garold (Gary) L. Johnson
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted