Jump to content

Recommended Posts

Posted

How to test if certain string can be converted to integer. That it is constructed only of numbers...

I need it for itoa.

Posted

Perhaps?

 

(defun numtxt  (str)
 (vl-every '(lambda (x) (<= 48 x 57)) (vl-string->list str)))

 

Returns T or nil

Posted

after dissection, very clever Lee...

Posted

Do you mean:

 

Command: (atoi "bla bla bla")
0

?

 

(defun atoi2(str)
 (if(distof str)(atoi str))
 ); end of atoi2

 

Command: (atoi2 "bla bla bla")
nil

 

Command: (atoi2 "8")
8

 

Command: (atoi2 "87.345123453")
87

Posted

Thanks!

 

Found in help later that atoi returns 0 if invalid string :oops: Although your solutions are great.

Posted

But I suppose its good to know if it is an invalid string as opposed to a user entering 0 :)

 

Anything else, just ask :)

Posted

Sometimes 0 would be a valid return...

 

On older way:

 

[b][color=BLACK]([/color][/b]defun testistr [b][color=FUCHSIA]([/color][/b]s / tmp[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq tmp [color=#2f4f4f]""[/color][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]repeat [b][color=NAVY]([/color][/b]strlen s[b][color=NAVY])[/color][/b]
         [b][color=NAVY]([/color][/b]setq tmp [b][color=MAROON]([/color][/b]strcat tmp [color=#2f4f4f]"#"[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]wcmatch s tmp[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

-David

Posted
Sometimes 0 would be a valid return...

 

On older way:

 

[b][color=BLACK]([/color][/b]defun testistr [b][color=FUCHSIA]([/color][/b]s / tmp[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq tmp [color=#2f4f4f]""[/color][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]repeat [b][color=NAVY]([/color][/b]strlen s[b][color=NAVY])[/color][/b]
         [b][color=NAVY]([/color][/b]setq tmp [b][color=MAROON]([/color][/b]strcat tmp [color=#2f4f4f]"#"[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]wcmatch s tmp[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

-David

 

 

Great idea David - love it. :) So many ways to accomplish the same thing :)

Posted

You might make use of this?

(vl-some '(lambda (x) (< 47 x 50)) (vl-string->list "abd1ef"))

Posted

(defun numtxt  (str)
 (vl-every '(lambda (x) (<= 48 x 57)) (vl-string->list str)))

Returns T or nil

 

CAB, would you say that vl-some should be used then, instead of vl-every?

Posted

Sorry, jumping in and out of this thread over several days, I forgot what was & wasn't posted.

This morning I revisited the entire thread & see the Lee had already posted this test. I also

see that the OP said the string was comprised of numbers only. My post like Lee's is just a test

to confirm that there is at lease one number embedded in the string. Not a solution for the OP.

 

I use this to convert a number from a string. (Must be a valid number)

 

  ;;+++++++++++++++++++++++++++++++
 ;;  convert the text to a number or nil
 ;;+++++++++++++++++++++++++++++++
 (defun txt2num (txt / num)
   (or (setq num (distof txt 5))
       (setq num (distof txt 2))
       (setq num (distof txt 1))
       (setq num (distof txt 4))
       (setq num (distof txt 3))
   )
   num
 )

 

Returns nil if not a valid number.

Posted

CAB,

You can avoid the allocation and destruction of a variable inside your support procedure by using a COND.

 

(defun txt2num ( txt )
 (cond
   ((distof txt 5))
   ((distof txt 2))
   ((distof txt 1))
   ((distof txt 4))
   ((distof txt 3))
 ) 
)

Posted

Doesn't distof work with 'REALs. I think the OP was for INTegers. as he wanted to use ITOA -David

Posted

I dunno, i didn't read the thread i just read the last post. ...wait a min? DISTOF works with strings and converts them to reals. ITOA converts an int to a string. Did you mean ATOI?

Posted

It looks like the problem has been answered several times now so were good.

Posted

Easy way:

 

(defun ITest(Str)
 (=(distof Str 2)(atoi Str))
 )

 

Testing:

 

_$ (ITest "5")
T
_$ (ITest "-50")
T
_$ (ITest "5.000008")
nil
_$ (ITest "5blablabla")
nil
_$ (ITest "0")
T

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...