Author Topic: Is there a better way?  (Read 3226 times)

0 Members and 1 Guest are viewing this topic.

daron

  • Guest
Is there a better way?
« 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.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Is there a better way?
« Reply #1 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??
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

DParcon

  • Guest
Is there a better way?
« Reply #2 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
 )


daron

  • Guest
Is there a better way?
« Reply #3 on: February 17, 2004, 09:49:58 AM »
Thanks. That helps a lot.

SMadsen

  • Guest
Is there a better way?
« Reply #4 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)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Is there a better way?
« Reply #5 on: February 17, 2004, 01:53:12 PM »
awwww.... I feel so speshul ...

Bein' how Stig cound a new word just for me....
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie