Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to get rid a NIL from a LIST.

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
devitg
1501 Views, 11 Replies

How to get rid a NIL from a LIST.

(setq sysvar-list(list (if (getvar "osmode")
        "osmode")
      (if (getvar "3dosmode")
        "3dosmode")
      (if (getvar "cmdecho")
        "cmdecho")
      (if (getvar "orthomode")
        "orthomode")
      (if (getvar "clayer")
        "clayer")))




 For a given acad version , it will return 

 

 

("osmode" nil "cmdecho" "orthomode" "clayer")

 I need it to be as follow 

 

("osmode" "cmdecho" "orthomode" "clayer")

 Thanks in advance

 

11 REPLIES 11
Message 2 of 12
hmsilva
in reply to: devitg

(setq lst '("osmode" "3dosmode" "cmdecho" "cmdecho" "clayer"))
(foreach l lst
(if (getvar l)
(setq sysvar-list (cons l sysvar-list))
)
)

HTH
Henrique

EESignature

Message 3 of 12
Lee_Mac
in reply to: devitg

(setq sysvar-list (vl-remove-if-not 'getvar '("osmode" "3dosmode" "cmdecho" "orthomode" "clayer")))
Message 4 of 12
devitg
in reply to: Lee_Mac

Thanks you both for the answer to my original post , it work as spected. But if I want to get rid of a a NIL at any list ? (setq list-w-nil (list 123 nil "qwerty" 678 ))
Message 5 of 12
Ajilal.Vijayan
in reply to: devitg


@devitg wrote:
But if I want to get rid of a a NIL at any list ? (setq list-w-nil (list 123 nil "qwerty" 678 ))

Then

 

(setq list-w-nil (vl-remove nil list-w-nil))

 

Message 6 of 12
Lee_Mac
in reply to: devitg

Or alternatively:

 

(vl-remove-if 'null <list>)
Message 7 of 12
hmsilva
in reply to: devitg


@devitg wrote:
Thanks you both for the answer to my original post , it work as spected. But if I want to get rid of a a NIL at any list ? (setq list-w-nil (list 123 nil "qwerty" 678 ))

You're welcome, devitg

 

Another

 

(setq list-w-nil (list 123 nil "qwerty" 678 ))
(foreach l list-w-nil
  (if l
    (setq list-w-no-nil (cons l list-w-no-nil))
  )
)

 

HTH

Henrique

 

EESignature

Message 8 of 12
Lee_Mac
in reply to: hmsilva

And another few Smiley Happy

 

(defun rem-nil1 ( l )
    (if l (if (car l) (cons (car l) (rem-nil1 (cdr l))) (rem-nil1 (cdr l))))
)
(defun rem-nil2 ( l )
    (apply 'append (mapcar '(lambda ( x ) (if x (list x))) l))
)
(defun rem-nil3 ( l )
    (apply 'append (subst nil '(nil) (mapcar 'list l)))
)
Message 9 of 12
hmsilva
in reply to: Lee_Mac


@Lee_Mac wrote:

And another few Smiley Happy

 


As always, fast reasoning... Smiley Very Happy

Nicely coded, Lee.

 

Henrique

EESignature

Message 10 of 12
Lee_Mac
in reply to: hmsilva

hmsilva wrote:
Lee_Mac wrote:

And another few Smiley Happy

 

As always, fast reasoning... Smiley Very Happy

Nicely coded, Lee.

 

Henrique

 

Thanks Henrique! Smiley Happy

Message 11 of 12
ehsan_bahrani
in reply to: Lee_Mac

hi

thank for your respond

how about removing nil Only from end of list?

for example this is my list:

(12 23 "aa"

15 87 Nil

43 19 "ba"

24 27 "ab"

Nil Nil Nil

Nil Nil Nil)

and i want to remove Nils from end of list. 

Message 12 of 12
hak_vz
in reply to: ehsan_bahrani


@ehsan_bahrani wrote:

hi

thank for your respond how about removing nil Only from end of list?

for example this is my list:

'(12 23 "aa" 15 87 Nil 43 19 "ba" 24 27 "ab" Nil Nil Nil Nil Nil Nil)and i want to remove Nils from end of list. 


(setq a '(12 23 "aa" 15 87 Nil 43 19 "ba" 24 27 "ab" Nil Nil Nil Nil Nil Nil))
(setq a (reverse a))
(while (not (car a)) (setq a (cdr a)))
(setq a (reverse a))

;-->  (12 23 "aa" 15 87 nil 43 19 "ba" 24 27 "ab")

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost