Author Topic: Trivial problem with function "setq" "read" "if"  (Read 1807 times)

0 Members and 1 Guest are viewing this topic.

Lupo76

  • Bull Frog
  • Posts: 343
Trivial problem with function "setq" "read" "if"
« on: June 18, 2014, 06:12:28 AM »
Hello to all,
I need to use the IF function with a constant variable.
I'm sorry but it's hard to explain.
Perhaps the following code helps

Code: [Select]
(setq trial (read (strcat "trial_" "1")))

Now if I write in AutoCAD: "!trial_1" (without quotes) is output 1.
So far so ok

If you then write

Code: [Select]
(if (= (read "trial_1") "1")
    (alert "true")
    (alert "false")
)

is issued "false". Why?
Where did I go wrong?

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Trivial problem with function "setq" "read" "if"
« Reply #1 on: June 18, 2014, 06:16:52 AM »
Try :

Code: [Select]
(if (= (eval (read "trial_1")) "1")
-David
R12 Dos - A2K

Lupo76

  • Bull Frog
  • Posts: 343
Re: Trivial problem with function "setq" "read" "if"
« Reply #2 on: June 18, 2014, 07:04:27 AM »
perfect! thank you!

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Trivial problem with function "setq" "read" "if"
« Reply #3 on: June 18, 2014, 07:19:05 AM »
Just to be clear for those who might not understand what's going on, read doesn't return the variable - it returns the quoted version of the symbol name. So the following 3 lines return the same thing:
Code - Auto/Visual Lisp: [Select]
  1. (read "my-var-name")
  2. (quote my-var-name)
  3. 'my-var-name
Note read only does this for the 1st form found in the string. A form can be a string (if escape quoted), a number (int/float), a symbol or a list:
Code - Auto/Visual Lisp: [Select]
  1. (read "1 2 3") ; Returns 1
  2. (read "var1 var2 var3") ; Returns VAR1
  3. (read "(var1 var2 var3)") ; Returns (VAR1 VAR2 VAR3)
In all cases the returned value is unevaluated.

Eval then takes the quoted symbol (or for that matter any Lisp syntax) and evaluates it to its result as if run.

So you get the following happening:
Code - Auto/Visual Lisp: [Select]
  1. (eval ; Will evaluate the following argument, then returns the result
  2.   (read ; Convert a string into a Lisp form to evaluate and returns this form
  3.     "trail_1" ; There's just this symbol as text, anything after it will be ignored
  4.   ) ; returns TRIAL_1, not the value inside it, but the quoted symbol
  5. ) ; returns the value inside the TRIAL_1 symbol
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

MeasureUp

  • Bull Frog
  • Posts: 462
Re: Trivial problem with function "setq" "read" "if"
« Reply #4 on: July 03, 2014, 08:41:35 PM »
Thanks to irneb.
Your helps to guys (like me) who wish to understand the autolisp are much appreciated.
BTW I have learned from your threads for years.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Trivial problem with function "setq" "read" "if"
« Reply #5 on: July 04, 2014, 12:45:51 AM »
Thanks to irneb.
Your helps to guys (like me) who wish to understand the autolisp are much appreciated.
BTW I have learned from your threads for years.
You're very welcome!  :-D
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

mailmaverick

  • Bull Frog
  • Posts: 493
Re: Trivial problem with function "setq" "read" "if"
« Reply #6 on: July 05, 2014, 11:23:56 AM »
Excellent explanation irneb !!!!

Great work !!!

Much appreciated !!!!!!