Lambda and getvar

Lambda and getvar

carlos_m_gil_p
Advocate Advocate
1,111 Views
8 Replies
Message 1 of 9

Lambda and getvar

carlos_m_gil_p
Advocate
Advocate

Hello boys how are you.

It will be that they give me a help with this.
I am trying to subtract the values ​​of some variables.

 

This is the list that I have.

 

(setq lista '("cmdecho" "osmode" "meshtype" "3dosmode" "orthomode"))
 
And I am trying to solve it in the following way.
 
 

(mapcar '(lambda (x)
   (vl-catch-all-apply 'getvar (cons (car x) (cadr x))))
 lista)

The result should be a new list like this.

 

(("cmdecho" . 1) ("osmode" . 1) ("meshtype" . 0) ("3dosmode" . 31) ("orthomode" . 0))



You must have vl-catch-all-apply in case the variable does not exist.

 

Thanks.

 

 


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Accepted solutions (2)
1,112 Views
8 Replies
Replies (8)
Message 2 of 9

pbejse
Mentor
Mentor

@carlos_m_gil_p wrote:

 

...You must have vl-catch-all-apply in case the variable does not exist.

 

Thanks.

 


Why the need for vl-catch-all-apply? if the variable does not exist then don't include it on the list to process, 

This should be enough.

 

(mapcar '(lambda (x) (cons x (getvar x))) lista)

 

Unless you meant something different by  "subtract", I'm assuming you meant "extract" the current values? 

If not, what to subtract the values with?

 

0 Likes
Message 3 of 9

Kent1Cooper
Consultant
Consultant
Accepted solution

@pbejse wrote:

 ...This should be enough.

 (mapcar '(lambda (x) (cons x (getvar x))) lista)

 

....

That was exactly what I came up with, and even if somehow a non-existent System Variable name gets into the lista variable, it doesn't cause any error, but the name simply ends up in a sub-list by itself, with no value paired with it.  Applying it to this list:

 

(setq lista '("whatever" "cmdecho" "osmode" "meshtype" "3dosmode" "orthomode"))

 

returns this:

 

(("whatever") ("cmdecho" . 1) ("osmode" . 163) ("meshtype" . 1) ("3dosmode" . 129) ("orthomode" . 0))

 

If something like that might happen, and you don't want such items included in the result, you can eliminate listed pseudo-variable names that return nil, for example this way [I'm sure there are other ways]:

 

(vl-remove nil (mapcar '(lambda (x) (if (getvar x) (cons x (getvar x)))) lista))

 

But where would a non-existent System Variable name come from to be in the list?

Kent Cooper, AIA
0 Likes
Message 4 of 9

pbejse
Mentor
Mentor
Accepted solution

@Kent1Cooper wrote:
...it doesn't cause any error, but the name simply ends up in a sub-list by itself, with no value paired with it.  Applying it to this list:

True, it does evaluate to nil.

 

Use foreach instead of mapcar to eliminate the non-existent System Variable name 

 

(setq lista '("cmdecho" "banana" "3dosmode" "osmode" "meshtype"   ))

(foreach itm lista
                       (if (setq f (getvar itm))
                           (setq nlist (cons
                                             (cons itm f) nlist)))
                       nlist)

(("meshtype" . 1) ("osmode" . 16495) ("3dosmode" . 139) ("cmdecho" . 1)) 

@Kent1Cooper wrote:

.... But where would a non-existent System Variable name come from to be in the list?


 

That is the question.

 

 

0 Likes
Message 5 of 9

_gile
Consultant
Consultant

Hi,

 

There's a nice routine to do all that stuff

 It's called ai_sysvar and it's part of acad20XXdoc.lsp (if I remeber well, it was in acad.mnl in older versions).

 

It's a nice example of using of defun-q to store the data within the defun-q list.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 9

_gile
Consultant
Consultant

@pbejse wrote:

@Kent1Cooper wrote:
...it doesn't cause any error, but the name simply ends up in a sub-list by itself, with no value paired with it.  Applying it to this list:

True, it does evaluate to nil.

 

Use foreach instead of mapcar to eliminate the non-existent System Variable name 

 

(setq lista '("cmdecho" "banana" "3dosmode" "osmode" "meshtype"   ))

(foreach itm lista
                       (if (setq f (getvar itm))
                           (setq nlist (cons
                                             (cons itm f) nlist)))
                       nlist)

(("meshtype" . 1) ("osmode" . 16495) ("3dosmode" . 139) ("cmdecho" . 1)) 

@Kent1Cooper wrote:

.... But where would a non-existent System Variable name come from to be in the list?


 

That is the question.

 

 


Be careful by hiding the errors this way.

Probably nobody should write "whatever" or "banana" but anyone can make a typo and write "odmode". In this case, we will not be warned of the typo, the routine can run without error but the "osmode" sysvar will never be reset.

 

IMO, the user (a developer in this case) have to be warned for such error.

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 9

pbejse
Mentor
Mentor

@_gile wrote:
Be careful by hiding the errors this way.

Probably nobody should write "whatever" or "banana" but anyone can make a typo and write "odmode". In this case, we will not be warned of the typo, the routine can run without error but the "osmode" sysvar will never be reset.

 

IMO, the user (a developer in this case) have to be warned for such error.

 


In that case, as a developer, The tool "Break on error" & "Last Break Source" in VLIDE is your friend.

 

Still begs the question of ...where would a non-existent System Variable name come from to be on the list if not from the developer?

 

 

EDIT: come to think of it, you are right, it will not show as it will not error., Guess its just good 'ol eagle eyes then 🙂

0 Likes
Message 8 of 9

ActivistInvestor
Mentor
Mentor

@pbejse wrote:


 

In that case, as a developer, The tool "Break on error" & "Last Break Source" in VLIDE is your friend.

 

Still begs the question of ...where would a non-existent System Variable name come from to be on the list if not from the developer?

 

 

EDIT: come to think of it, you are right, it will not show as it will not error., Guess its just good 'ol eagle eyes then 🙂


System variables can be product-specific, or even machine/user-specific.

 

They can be defined in the registry.

0 Likes
Message 9 of 9

carlos_m_gil_p
Advocate
Advocate

 

Hi, pbejse.
Thanks for the correction, I wanted to say "extract"
Excuse my English, I only speak Spanish.

 

I used this method.

(mapcar '(lambda (x) (cons x (getvar x))) lista)

This is a good option.

(foreach itm lista
                       (if (setq f (getvar itm))
                           (setq nlist (cons
                                             (cons itm f) nlist)))
                       nlist)

Hello Kent1Cooper.
Thanks for your help, as always.

 

It has happened to me that I have found variables in new AutoCAD that do not exist in old AutoCADs.

That's why I wanted to solve this error.

 

Thank you for your proposal.

(vl-remove nil (mapcar '(lambda (x) (if (getvar x) (cons x (getvar x)))) lista))

Hi _gile.

I'm going to study what you're commenting too.

 

In general, thank you very much everyone for your help and comments.

Greetings.

 


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes