TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: andrew_nao on January 05, 2012, 03:13:13 PM

Title: distiguish variable between a number or a word
Post by: andrew_nao on January 05, 2012, 03:13:13 PM
hello all and happy new year.

my first request for the new year  :-o

can anyone help me with how i can get code to tell the difference between a variable being words or numbers?

example

(if var = a word
 (do this)
)
(if var = a number
(do this)
)

any help is appreciated
Title: Re: distiguish variable between a number or a word
Post by: alanjt on January 05, 2012, 03:40:17 PM
You could check with the type function - if I'm understanding correctly.

Code: [Select]
Command: (type 3.4)
REAL

Command: (type 3)
INT

Command: (type "3.4")
STR
Title: Re: distiguish variable between a number or a word
Post by: andrew_nao on January 05, 2012, 04:05:40 PM
thanks for the reply Alan, however my variables will always be in quotes so it will come up as a string and not an integer
Title: Re: distiguish variable between a number or a word
Post by: alanjt on January 05, 2012, 04:08:31 PM
Try distof...

Code: [Select]
Command: (distof "3.4")
3.4

Command: (distof "blah")
nil
Title: Re: distiguish variable between a number or a word
Post by: andrew_nao on January 05, 2012, 04:11:45 PM
thanks again but that isnt working either

my number is returned as a string and looks like this
"97 01000 000 16 99 10"

and distof returns nil
Title: Re: distiguish variable between a number or a word
Post by: alanjt on January 05, 2012, 04:14:52 PM
Give me a few examples to go on - as word and as number.
Title: Re: distiguish variable between a number or a word
Post by: andrew_nao on January 05, 2012, 04:26:16 PM
Give me a few examples to go on - as word and as number.


 a word, for instance, would be
"plate" or "pipe".   just a plain word.

and a number would be
"97 01000 000 16 99 10"

the number would be exactly like you see it here but with different digits
same spacing format.

both would be returned via my existing code as strings
Title: Re: distiguish variable between a number or a word
Post by: Jeff_M on January 05, 2012, 04:48:23 PM
A fairly simplistic solution for this specific case:
Code: [Select]
(if (wcmatch str "*@*")
  (princ "\nIt is a string.")
  (princ "\nIt is a number")
)
Title: Re: distiguish variable between a number or a word
Post by: fixo on January 05, 2012, 04:51:06 PM
just for the single words without digits (not for "cloud number 9" etc):
Code: [Select]
(if
(vl-some '(lambda(x)
(or (distof (chr x))(eq 32 x)))
(vl-string->List StringExpression))
(alert "number")
(alert "word"))
Title: Re: distiguish variable between a number or a word
Post by: ronjonp on January 05, 2012, 04:51:50 PM
A fairly simplistic solution for this specific case:
Code: [Select]
(if (wcmatch str "*@*")
  (princ "\nIt is a string.")
  (princ "\nIt is a number")
)

Nice  8-)
Title: Re: distiguish variable between a number or a word
Post by: alanjt on January 05, 2012, 06:48:39 PM
A fairly simplistic solution for this specific case:
Code: [Select]
(if (wcmatch str "*@*")
  (princ "\nIt is a string.")
  (princ "\nIt is a number")
)
There you go. :-)

Nice  8-)
Title: Re: distiguish variable between a number or a word
Post by: andrew_nao on January 09, 2012, 10:44:35 AM
thanks everyone
both solutions work perfectly

didnt think it was that easy... :ugly: