chavlji Posted March 21, 2009 Share Posted March 21, 2009 How to test if certain string can be converted to integer. That it is constructed only of numbers... I need it for itoa. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted March 21, 2009 Share Posted March 21, 2009 Perhaps? (defun numtxt (str) (vl-every '(lambda (x) (<= 48 x 57)) (vl-string->list str))) Returns T or nil Quote Link to comment Share on other sites More sharing options...
lpseifert Posted March 21, 2009 Share Posted March 21, 2009 after dissection, very clever Lee... Quote Link to comment Share on other sites More sharing options...
ASMI Posted March 21, 2009 Share Posted March 21, 2009 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 Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted March 21, 2009 Share Posted March 21, 2009 Cheers Larry Quote Link to comment Share on other sites More sharing options...
chavlji Posted March 21, 2009 Author Share Posted March 21, 2009 Thanks! Found in help later that atoi returns 0 if invalid string Although your solutions are great. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted March 21, 2009 Share Posted March 21, 2009 But I suppose its good to know if it is an invalid string as opposed to a user entering 0 Anything else, just ask Quote Link to comment Share on other sites More sharing options...
David Bethel Posted March 22, 2009 Share Posted March 22, 2009 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 Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted March 22, 2009 Share Posted March 22, 2009 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 Quote Link to comment Share on other sites More sharing options...
CAB Posted March 23, 2009 Share Posted March 23, 2009 You might make use of this? (vl-some '(lambda (x) (< 47 x 50)) (vl-string->list "abd1ef")) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted March 23, 2009 Share Posted March 23, 2009 (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? Quote Link to comment Share on other sites More sharing options...
CAB Posted March 23, 2009 Share Posted March 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
Se7en Posted March 23, 2009 Share Posted March 23, 2009 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)) ) ) Quote Link to comment Share on other sites More sharing options...
David Bethel Posted March 23, 2009 Share Posted March 23, 2009 Doesn't distof work with 'REALs. I think the OP was for INTegers. as he wanted to use ITOA -David Quote Link to comment Share on other sites More sharing options...
Se7en Posted March 23, 2009 Share Posted March 23, 2009 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? Quote Link to comment Share on other sites More sharing options...
Se7en Posted March 23, 2009 Share Posted March 23, 2009 It looks like the problem has been answered several times now so were good. Quote Link to comment Share on other sites More sharing options...
ASMI Posted March 23, 2009 Share Posted March 23, 2009 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.