Page 1 of 2 12>
Topic Options
#136534 - 2005-03-28 04:03 PM IsNumeric RFC..
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Playing around with Howard's code which seems to be about the best I could find out there..
Code:

CLS
BREAK ON
;http://www.kixtart.org/ubbthreads/showthreaded.php?Cat=&Number=109066
;http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Board=UBB1&Number=126912
$text = "8", "a", "-12"
;$Pattern='^\d{1,3}$'
;This would match any 1,2, or 3 digit number.
;After check to verify that the the input passes this test
; do a simple check for greater then 0 and less then 266.
for each $item in $text
if RegExpTest("^\d{1,3}$", $item, 1)
? $item + "-Found match"
else
? $item + "-No Match found"
endif
next
get $
Function RegExpTest($Pattern, $String, $IgnoreCase)
Dim $regEx, $Match, $Matches ; Create variable.
$regEx = createobject("VBscript.RegExp") ; Create a regular expression.
$regEx.Pattern = $Pattern ; Set pattern.
$regEx.IgnoreCase = val($IgnoreCase) ; Set case insensitivity.
$RegExpTest = $regEx.Test($String) ; Execute test search.
EndFunction



The problem is that it does not seem to handle negative values very well.

Thanks,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#136535 - 2005-03-28 04:15 PM Re: IsNumeric RFC..
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
We can change that behavior by permitting an optional minus sign as the starting character. If the pattern to match was changed to lets say...

"^-?\d{1,3}$"

I do not believe that the hyphen is a metacharcter so this should work. If it does not then add a backslash before the hyphen to escape it.

("^\-?\d{1,3}$"

Quote:

Quantifier Description
{num} Matches the preceding element num times.
{min, max} Matches the preceding element at least min times, but not more than max times.
? Matches any preceding element 0 or 1 times.
* Matches the preceding element 0 or more times.
+ Matches the preceding element 1 or more times.




_________________________
Home page: http://www.kixhelp.com/hb/

Top
#136536 - 2005-03-28 04:46 PM Re: IsNumeric RFC..
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
A more generic pattern would be "^-?\d+$" this should match any string that is one or more numerics optionally preceded by a minus sign. The example above that uses {1,3} only match a numeric string that is a minimum of 1 to a maximum of 3 numeric characters in length.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#136537 - 2005-03-28 05:05 PM Re: IsNumeric RFC..
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
and "^-?[,\d{1,3}]+$" would match positive or negative numerics that contained commas.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#136538 - 2005-03-28 09:19 PM Re: IsNumeric RFC..
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Thanks Howard! That works a treat..

Did some digging through some old code and found:
Code:

;Function ISNUMERIC()
;Author Kent Dyer (leptonator@hotmail.com)
;Action Check for a numeric value
;Syntax ISNUMERIC("value")
;Version 1.0
;Date 09 - March - 2004
;Date Revised 09 - March - 2004
;Parameters value
; Value to test for
;Remarks This is a simple check for a numeric value.
;Returns Check for the value being a numeric
;Dependencies None
;KiXtart Ver 4.02
;Example(s) ?ISNUMERIC('123342334')
; ?ISNUMERIC('test')
;
?ISNUMERIC('-123342334')
?ISNUMERIC('123342334')
?ISNUMERIC('test')
get $
FUNCTION ISNUMERIC($val)
DIM $i,$res[1],$result
IF CINT($val)
$res='Integer'
ELSE
$res='nonInteger'
ENDIF
$ISNUMERIC=$res
ENDFUNCTION



Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#136539 - 2005-03-28 10:33 PM Re: IsNumeric RFC..
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Kent, I think that function does not provide the level of checking you may need. try:

Code:
? "1test =  " + ISNUMERIC('1test')  



My result:
Quote:

1test = Integer




Edited by Howard Bullock (2005-03-28 10:34 PM)
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#136540 - 2005-03-28 11:19 PM Re: IsNumeric RFC..
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
I was thing about this.. If you take a var and raise it to a zero power, you still get 1. Problem with that though is if you think about it from a Algebraic term, you will still get a one.

So, agreed - your method works much better.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#136541 - 2005-03-29 01:33 AM Re: IsNumeric RFC..
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
ISNUMERIC will also need to cover decimals, floats, and so on and not be limited to integers.
_________________________
There are two types of vessels, submarines and targets.

Top
#136542 - 2005-03-29 06:28 AM Re: IsNumeric RFC..
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Then.. I guess it would be interesting to see what the code is behind the VB implementation..

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#136543 - 2005-03-29 09:08 AM Re: IsNumeric RFC..
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
so what's wrong with something simple as:
Code:

function isNumeric($var)
if $var=0.1+$var-0.1
$isNumeric = 1
endif
endfunction



Edited by Lonkero (2005-03-29 03:50 PM)
_________________________
!

download KiXnet

Top
#136544 - 2005-03-29 03:48 PM Re: IsNumeric RFC..
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
That code does absolutely nothing, for me at least. what's with commas? Is that supposed to be an iif()?
Top
#136545 - 2005-03-29 03:50 PM Re: IsNumeric RFC..
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
sorry, updated.
forgot that kixtart is american and not european breed.
_________________________
!

download KiXnet

Top
#136546 - 2005-03-29 04:05 PM Re: IsNumeric RFC..
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
...and i forgot that the european breed uses commas. Makes much more sense now and it works. nice idea.
Top
#136547 - 2005-03-29 04:19 PM Re: IsNumeric RFC..
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Jooel, here's another twist on your UDF ... actually returns Booleans ...


Code:

function isNumeric($var)
$IsNumeric = iif($var=0.0+$var,NOT 0,NOT 1)
endfunction


Top
#136548 - 2005-03-29 04:37 PM Re: IsNumeric RFC..
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
fine!
so I golf it:
Code:

function isNumeric($var)
$IsNumeric = not iif($var=0.0+$var,1,0)
endfunction

_________________________
!

download KiXnet

Top
#136549 - 2005-03-29 05:37 PM Re: IsNumeric RFC..
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Jooel got it!

Shouldn't you create a UDF for this?

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#136550 - 2005-03-29 05:49 PM Re: IsNumeric RFC..
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
He just did. If it starts with "Function" and ends with "EndFunction", it is a UDF.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#136551 - 2005-03-29 05:49 PM Re: IsNumeric RFC..
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Anything wrong with

Code:

function isNumeric($var)
$IsNumeric = ($var=0.0+$var)
endfunction


Top
#136552 - 2005-03-29 05:50 PM Re: IsNumeric RFC..
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
good point les
_________________________
!

download KiXnet

Top
#136553 - 2005-03-29 05:52 PM Re: IsNumeric RFC..
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
not sure if there is something wrong with it, or:
Code:

function isNumeric($var)
$IsNumeric = $var=0.0+$var
endfunction

_________________________
!

download KiXnet

Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 507 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.076 seconds in which 0.029 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org