TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: daron on February 16, 2004, 06:27:26 PM

Title: Is there a better way?
Post by: daron on February 16, 2004, 06:27:26 PM
I have this:
Code: [Select]
(setq var '("osmode" "clayer" "cmdecho"
"ltscale" "plinewid" "users1"
"users2" "users3")
  val  (mapcar 'getvar var)
  )

and the only way I've found to get single values from single variables from anywhere in the code is this:
Code: [Select]

     (setq num 0)
     (foreach item var
 (if (/= item "users3")
     (setq num (1+ num))
     (setq angv (nth num val))
     )
 )

I feel that a mapcar lambda should do the trick, but I couldn't get that to work.
Title: Is there a better way?
Post by: Keith™ on February 16, 2004, 07:22:16 PM
Ok try this to set the variables in the list .....

Code: [Select]

(setq var '("osmode" "clayer" "cmdecho" "ltscale" "plinewid" "users1" "users2" "users3")
      val (apply 'append (mapcar '(lambda (x) (list (cons (read x) (getvar x)))) var))
)


and this to retrieve a specific variable

Code: [Select]

(cdr (assoc 'USERS1 val))


The reason why I used (read x) is because it returns a string that is all caps and treated as an atom and is therefore able to be recognized regardless of capitalization of the request for value ...

hence..
Code: [Select]

(cdr (assoc 'USERS1 val))
(cdr (assoc 'users1 val))
(cdr (assoc 'UsErS1 val))


will all return the correct value.

If you attempt to do the following:

Code: [Select]

(cons X (getvar x))


you will have to match capitalization.

Code: [Select]

(cdr (assoc "users1" val))


Does this make sense??
Title: Is there a better way?
Post by: DParcon on February 16, 2004, 07:43:53 PM
Will this help? (test "users1")


Code: [Select]


(defun test (x / val var)
    (setq var '("osmode" "clayer" "cmdecho"
              "ltscale" "plinewid" "users1"
              "users2" "users3")
    )
    (if (and (= (type x) 'STR)
             (setq var (member x var))
        )
      (setq val (getvar (car var)))
    )
    val
 )

Title: Is there a better way?
Post by: daron on February 17, 2004, 09:49:58 AM
Thanks. That helps a lot.
Title: Is there a better way?
Post by: SMadsen on February 17, 2004, 10:30:26 AM
(nth (vl-position x var) val)

or ..

(nth (vl-position (strcase x)(mapcar 'strcase var)) val)

or .. Keith'ified

(nth (vl-position (read x) (mapcar 'read var)) val)
Title: Is there a better way?
Post by: Keith™ on February 17, 2004, 01:53:12 PM
awwww.... I feel so speshul ...

Bein' how Stig cound a new word just for me....