Author Topic: Variable value within a list  (Read 1996 times)

0 Members and 1 Guest are viewing this topic.

DanB

  • Bull Frog
  • Posts: 367
Variable value within a list
« on: January 30, 2020, 11:56:50 AM »
I'm struggling with applying a defined value and sending to a list. The list does not reflect my desired results. I've never been good with bound and local variables. Suggestions?

Undesired result =(("Val1" VALUE1) ("Val2" VALUE2))
Desired result =(("Val1" 10.22) ("Val2" 99.99))

example
Code: [Select]
(defun uservalues (/ saved_values)
 (setq saved_values
  '(("Val1" value1)
    ("Val2" value2)
   ) ; ends list of values
 ); ends setq
) ; end defun

(defun c:TEST (/)
 (setq value1 "10.22")
 (setq value2 "99.99")
 (uservalues)
)




JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: Variable value within a list
« Reply #1 on: January 30, 2020, 12:21:53 PM »
A little vague: are you talking about custom variables and values (like an association list) or are you talking about AutoCAD settings/variables?

This snip comes to you from the member ElpanovEvgeniy and has to do with storing and setting AutoCAD settings/variables. However, you can use a similar method for your own variables. ...or use association lists. So many options.

Code - Auto/Visual Lisp: [Select]
  1.      ERROR-LST-
  2.      '("AUTOSNAP" "OSMODE" "APERTURE" "HPSPACE" "HPASSOC"
  3.        "MIRRTEXT" "AUPREC" "LUPREC" "DIMZIN" "CECOLOR" "CLAYER"
  4.        "CMDECHO" "FILEDIA" "OSMODE")
  5.      ERROR-LST- (mapcar (function (lambda (a) (list 'setvar a (getvar a)))) ERROR-LST-)
  6.      )
  7. (mapcar 'eval ERROR-LST-)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

DanB

  • Bull Frog
  • Posts: 367
Re: Variable value within a list
« Reply #2 on: January 30, 2020, 12:33:02 PM »
I'm trying to write a routine involving a dialog box. I have a function that sets default values for the edit boxes. I then want to store any user changes so when the dialog is opened repeatedly the user sees their latest values, not the defaults. Below was an overly simplified example of how I attempted to pass the user values to a list.

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: Variable value within a list
« Reply #3 on: January 30, 2020, 12:58:31 PM »
Oh? Well, then you want to save the variable/values to a file. Without specifics, below is an example of how I saved the layers, for the drawing, to a file and how I read them back into a list which I could use in my program (simple example but a little more applicable).

Code - Auto/Visual Lisp: [Select]
  1. (defun save-layer-list-to-file ( / x f fp )
  2.   ;; retrieve all layers from dwg dict and save
  3.   ;; to a file.  file saved where drawing is located
  4.       (setq f (strcat (getvar 'DWGPREFIX) (getvar 'DWGNAME) ".la")
  5.             fp (open f "A"))
  6.       (while (setq x (tblnext "LAYER" (not x)))
  7.              (cond
  8.                ((not (>= (cdr (assoc 70 x)) 16))
  9.                 (prin1 x fp)
  10.                 (princ "\n" fp)) )
  11.              )
  12.       (close fp)
  13.       (princ) )
  14.  
  15. (defun get-list-from-file ( name / fp lst read-file-into-list )
  16.   ;; general file reader
  17.   ;; given a file name this procedure will read the contents
  18.   ;; into a list
  19.          (defun read-file-into-list ( str file )
  20.            (if str
  21.              (cons
  22.                (if (not (wcmatch str "*;*")) str "")
  23.                ;; get string from file; if it is a comment, return an empty string.
  24.                (read-file-into-list (read-line file) file))))
  25.   (setq fp (open name "R"))
  26.   (setq lst (read-file-into-list (read-line fp) fp))
  27.   (close fp)
  28.  lst
  29. )
  30.  
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Variable value within a list
« Reply #4 on: January 30, 2020, 01:08:52 PM »
I'm struggling with applying a defined value and sending to a list. The list does not reflect my desired results. I've never been good with bound and local variables. Suggestions?

Undesired result =(("Val1" VALUE1) ("Val2" VALUE2))
Desired result =(("Val1" 10.22) ("Val2" 99.99))

The key is the use of the apostrophe/single-quote: this operator instructs the AutoLISP interpreter to interpret what follows as literal data, not requiring evaluation. Consequently, the symbols value1 & value2 are not evaluated to yield the values they hold, but rather remain as symbols. I delve into this in far more detail with several examples in my tutorial on The Apostrophe and the Quote Function.

For your particular example, the solution would be to construct the list using the list function, which evaluates its supplied arguments:
Code - Auto/Visual Lisp: [Select]
  1. (defun uservalues ( / saved_values )
  2.     (setq saved_values
  3.         (list
  4.             (list "Val1" value1)
  5.             (list "Val2" value2)
  6.         )
  7.     )
  8. )
  9.  
  10. (defun c:TEST ( / )
  11.    (setq value1 "10.22")
  12.    (setq value2 "99.99")
  13.    (uservalues)
  14. )

Note that it may be better practice to supply the values as arguments to the uservalues function, and then assign the variable saved_values to the return of this function, as in your current code, the variables value1 and value2 are global. For example:
Code - Auto/Visual Lisp: [Select]
  1. (defun uservalues ( v1 v2 )
  2.     (list
  3.         (list "Val1" v1)
  4.         (list "Val2" v2)
  5.     )
  6. )
  7.  
  8. (defun c:TEST ( / value1 value2 )
  9.    (setq value1 "10.22")
  10.    (setq value2 "99.99")
  11.    (setq saved_values (uservalues value1 value2))
  12. )

This is obviously a very contrived example.

DanB

  • Bull Frog
  • Posts: 367
Re: Variable value within a list
« Reply #5 on: January 30, 2020, 01:15:43 PM »
Thank you both for your assistance. I think this can get me back on track.

BIGAL

  • Swamp Rat
  • Posts: 1411
  • 40 + years of using Autocad
Re: Variable value within a list
« Reply #6 on: January 31, 2020, 09:22:09 PM »
In a dcl you can set default value so either as mentioned save in a txt file or for this session make it a global variable but be careful about its name. 

(if (not AH:but)(setq AHbut 1)) then call dcl.

There is some defaults in the dwg you can use Useri1-5 integers, Userr1-5 reals, Users1-5 strings, these get saved in the dwg, but can be used by any one.

Another is (setenv "myval1" 12)  (Getenv "myval1")

« Last Edit: January 31, 2020, 09:26:45 PM by BIGAL »
A man who never made a mistake never made anything