Author Topic: distiguish variable between a number or a word  (Read 2479 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
distiguish variable between a number or a word
« 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

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: distiguish variable between a number or a word
« Reply #1 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
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

andrew_nao

  • Guest
Re: distiguish variable between a number or a word
« Reply #2 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

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: distiguish variable between a number or a word
« Reply #3 on: January 05, 2012, 04:08:31 PM »
Try distof...

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

Command: (distof "blah")
nil
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

andrew_nao

  • Guest
Re: distiguish variable between a number or a word
« Reply #4 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

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: distiguish variable between a number or a word
« Reply #5 on: January 05, 2012, 04:14:52 PM »
Give me a few examples to go on - as word and as number.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

andrew_nao

  • Guest
Re: distiguish variable between a number or a word
« Reply #6 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

Jeff_M

  • King Gator
  • Posts: 4098
  • C3D user & customizer
Re: distiguish variable between a number or a word
« Reply #7 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")
)

fixo

  • Guest
Re: distiguish variable between a number or a word
« Reply #8 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"))

ronjonp

  • Needs a day job
  • Posts: 7531
Re: distiguish variable between a number or a word
« Reply #9 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-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: distiguish variable between a number or a word
« Reply #10 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-)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

andrew_nao

  • Guest
Re: distiguish variable between a number or a word
« Reply #11 on: January 09, 2012, 10:44:35 AM »
thanks everyone
both solutions work perfectly

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