Author Topic: How lisp tell if a string is a number?  (Read 17720 times)

0 Members and 1 Guest are viewing this topic.

Daniel J. Ellis

  • Swamp Rat
  • Posts: 811
How lisp tell if a string is a number?
« on: March 24, 2009, 09:58:50 AM »
I'm working on updating a plot renumbering routine I wrote about a year ago.

We always put plot numbers on their own layer, so the routine originally worked by processing all of the objects on that layer.

This works OK, unless the plot number text isn't actually a number (for example, somone might want to note a binstore).

Is there a way to test whether the contents of a string are actually a number?

dJE
===
dJE

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How lisp tell if a string is a number?
« Reply #1 on: March 24, 2009, 10:04:09 AM »
Code: [Select]
(defun _DistOf ( x / result fixed )

[color=green]    ;;  Convert a string to a numerical value if possible. Also, if it
    ;;  can be determined that the intent of the string representation
    ;;  is that the number is to be an integer rather than an a real an
    ;;  integer is returned.
[/color]
    (vl-some
       '(lambda ( base ) (setq result (distof x base)))
       '(2 1 5 3 4)
    )

    (if result
        (if (vl-string-position 46 x)
            result [color=green]    ;; assume result is a real[/color]
            (if (eq result (setq fixed (fix result)))
                fixed  [color=green];; assume result is an integer[/color]
                result [color=green];; assume result is a real[/color]
            )
        )
    )
)

Code: [Select]
(if (setq dist (_DistOf (setq str "123.2")))
    (_DoStuffWithDist dist)
    (_DoStuffWithStr str)
)

Modify to suit. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How lisp tell if a string is a number?
« Reply #2 on: March 24, 2009, 10:04:49 AM »
Hi,

(numberp (read str))
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How lisp tell if a string is a number?
« Reply #3 on: March 24, 2009, 10:14:57 AM »
Funny you should ask as this was just discussed on another forum.

Like Michael's, this will return a number or nil
Code: [Select]
(defun txt2num ( txt )
  (cond
    ((distof txt 5))
    ((distof txt 2))
    ((distof txt 1))
    ((distof txt 4))
    ((distof txt 3))
  )
)

This will test for a number within a string. (not what you want)
Code: [Select]
(vl-some '(lambda (x) (< 47 x 50)) (vl-string->list "your string 2"))
I like gile's test.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How lisp tell if a string is a number?
« Reply #4 on: March 24, 2009, 10:19:41 AM »
I suppose we should ask for some typical data.

For example if the candidate values are always simple ints or floats (123 | 123.4) then the _DistOf function I provided is overkill, as (numberp (read "123")) -> T or (numberp (read "123.4")) -> T.

However, if the data may have fractions etc. that need to be properly interpreted as numerical values then (_DistOf "1-1/2\"") => 1.5 would be the route to go as (numberp (read "1-1/2\"")) would return nil.

PS: Nicely done Gile. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How lisp tell if a string is a number?
« Reply #5 on: March 24, 2009, 10:20:16 AM »
Funny you should ask as this was just discussed on another forum.

traitor :P
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How lisp tell if a string is a number?
« Reply #6 on: March 24, 2009, 10:35:48 AM »
I don't view our neighbors as enemies.
So don't blow my cover as a spy. 8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How lisp tell if a string is a number?
« Reply #7 on: March 24, 2009, 10:36:29 AM »
This also generates an error
(numberp (read ".5   "))  
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: How lisp tell if a string is a number?
« Reply #8 on: March 24, 2009, 10:38:58 AM »
I suppose we should ask for some typical data.
Yep, that would help. :)
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: How lisp tell if a string is a number?
« Reply #9 on: March 24, 2009, 10:39:40 AM »
Code: [Select]
(numberp (read "11BN"))
nil
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How lisp tell if a string is a number?
« Reply #10 on: March 24, 2009, 10:40:13 AM »
I don't view our neighbors as enemies.
So don't blow my cover as a spy. 8-)

forum slut

there, I said it

:lmao:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How lisp tell if a string is a number?
« Reply #11 on: March 24, 2009, 10:41:13 AM »
Code: [Select]
(numberp (read "11BN")) => nil

But ... should "11BN" be interpreted as a number? I'm not thinking so. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How lisp tell if a string is a number?
« Reply #12 on: March 24, 2009, 10:45:54 AM »
Hey if you like sex what better than to sleep around. ;-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How lisp tell if a string is a number?
« Reply #13 on: March 24, 2009, 10:49:09 AM »
If ya can't be good might as well be bad. :evil:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Daniel J. Ellis

  • Swamp Rat
  • Posts: 811
Re: How lisp tell if a string is a number?
« Reply #14 on: March 24, 2009, 04:23:59 PM »
MP, I'm sorry, that's way beyond me.  Do all possible numbers need entering into the list-I can't believe that would be the case?  If so that would rule out its use here (it would leaving me needing to create a random? list up to 1000).  I can find evidence that VL-SOME is an autolisp function,  but nothing to suggest what it might do.  What do FIXED and RESULT need to be set to?  How do distof, x, and base get generated?  I assume one of them is the test string?

CAB, I assume your use of DISTOF is the one MP created?  Similarly or seem to need to process through all possible values?

Gile, this seems to be closest to what I want.  I assume that this will work on an non-number string without crashing?  Would I use it:
<code>
(IF
    (numberp (READ txt))
    (SETQ txt2 (ATOI txt))
)
</code>

Typical data are plot numbers on an housing estate, so values are always integers, starting from 1 up to about 600.  In terms of settings I'm going up to about 1000, purely because I can't imagine a situation where a greater value would be required/encountered.  I will go on to manipulate the number (comparison, addition or subtraction)  I just want to ensure the string is indeed a number to avoid errors and crashed.

dJE
===
dJE