Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

wcmatch help

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
markruys2
796 Views, 13 Replies

wcmatch help

how to determine if a string is a valid floating number, ie does not contain any invalid chars etc

i came out with this, but seems too complicated, i am suspecting an easier solution

(setq str "11.25")

(wcmatch str "*@*,[`~```@`#`$`%`^`&`*`(`)`-`_`+`=`[`]`{`}`|`\`;`:`\"`'`,`<`>`?`/")

TIA

13 REPLIES 13
Message 2 of 14
SomeBuddy
in reply to: markruys2

Hi,

 

You could use something like this:

 

 

(defun chkval (str)
  (if (numberp (read str))
    (alert "It's a number")
    (alert "It's not a number")
  )
  (princ)
)

Usage:

 

(chkval "11.25")

(chkval "11.qr")

 

HTH

 

 

Message 3 of 14
markruys2
in reply to: SomeBuddy

it helps, although i am intigued how a string like "12,44"  a comma instead of a period is handled correctly by lisp,

(read "12,44") returns 12 but then numberp function is able to reject it.

it works, thanks

m

Message 4 of 14
SomeBuddy
in reply to: markruys2

You're welcome.

 

(read "12,44") returns 12,44 on my machine and this is not a valid number for the LISP interpretor, since it "knows" that the decimal separator is the period and not the comma.

Message 5 of 14
Kent1Cooper
in reply to: SomeBuddy


@SomeBuddy wrote:

.... 

(defun chkval (str)
  (if (numberp (read str))
    (alert "It's a number")
    (alert "It's not a number")
  )
  (princ)
)

....


Might the strings ever include spaces?  For instance:

 

(chkval "30 days")

 

says it's a number.

Kent Cooper, AIA
Message 6 of 14
SomeBuddy
in reply to: Kent1Cooper

Hmm,

 

This (read...) function is so dumb. The following will do better, except for the case where the string is entered using the scientific units type, i.e. using the "e-" or "e+" notation, which is quite rare:

 

 

(defun chknum (str)
  (if
    (vl-remove-if
     '(lambda (x)(or (wcmatch (chr x) "#")(= (chr x) ".")))
      (vl-string->list str)
    )
    (alert "It's not a number.")
    (alert "It's a number.")    
  )
  (princ)
)

 

 

Message 7 of 14
markruys2
in reply to: SomeBuddy

 

had to add to the if condition

 

(if  (and ...

(< (length (vl-remove-if-not '(lambda (x)(= x 46))(vl-string-list str))) 2)   ...

 

to allow only 1 period or no period

still think there should be an easier way, a combo of rtos / atof / distof /atoi / itoa ?

Message 8 of 14
SomeBuddy
in reply to: markruys2

I understand the two periods issue, but I don't understand the no period one. Maybe this one is good enough:

 

(defun chknum (str)
  (if (not (distof str))
    (alert "It's not a number.")
    (alert "It's a number.")    
  )
  (princ)
)

 

 

 

 

Message 9 of 14
Kent1Cooper
in reply to: SomeBuddy


@SomeBuddy wrote:

I understand the two periods issue, but I don't understand the no period one. Maybe this one is good enough:

 

....
  (if (not (distof str))
....

No periods = integers, which they do want to allow.  But testing for quantity of periods doesn't appear to be necessary, given that (distof) seems to be the magic function that does the trick [at least, I haven't hit on any type of string content that fools it.]

Kent Cooper, AIA
Message 10 of 14
markruys2
in reply to: Kent1Cooper

some minor issues w/ distof ... a leading space,  inch/foot signs, forward slashes (of course depending on the units setting)

 

to make clear my intentions, i am trying to test input in a dialog edit box for a valid number, be it integer or float.

 

thank you Kent & Some

m

Message 11 of 14
SomeBuddy
in reply to: Kent1Cooper

<No periods = integers, which they do want to allow.>

 

I know what no period means, but what I was saying is that I don't see why is this alternative mentionned as an issue, while for me, the solution posted at that time was doing fine with integers.

Message 12 of 14
SomeBuddy
in reply to: markruys2

You're welcome. I was supposing that this is what you want to do with it, i.e. validating some user input 🙂

Message 13 of 14
Kent1Cooper
in reply to: SomeBuddy


@SomeBuddy wrote:

....

I know what no period means, but what I was saying is that I don't see why is this alternative mentionned as an issue, while for me, the solution posted at that time was doing fine with integers.


Integers weren't an "issue," but one of the approved conditions ["allow only 1 period or no period"].  It was only a string with more than one period that was the issue, because that routine considered such a string to represent a number.  Their added check was only to ensure that such a string would not pass.

Kent Cooper, AIA
Message 14 of 14
SomeBuddy
in reply to: markruys2

The inch/foot signs and the forward slashes should be controlled using the proper mode (units settings) but for the leading  space(s) you can improuve the code a little bit using something like:

 

 

(if (distof (vl-string-trim " " str))
  (alert "It's a number.")
  (alert "It's not a number.")
)

 Regards

 

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost