Remove item from list/ add item to a list

Anonymous

Remove item from list/ add item to a list

Anonymous
Not applicable

hi all,

i have the following question,

(setq a (5.2, 25))

(setq b (12.3. 56))

(setq c (15.3,16.2))

(setq d (18.3,17.9))

(command "pline" a b c d "")

 

suppose the values in setq c and setq d is zero or nil, then (command "pline" a b c d) will be error. so, i have to remove c and d (because its value is nil) from the list by checking its value and make the command as (command "pline" a b ""). so, how can i make this? please help me. am using autocad 2017.

 

thanks in advance

 

 

 

 

 

0 Likes
Reply
Accepted solutions (4)
3,876 Views
11 Replies
Replies (11)

dbroad
Mentor
Mentor

First: Be sure to post AutoLISP questions in the AutoLISP customization forum.

Second:  Take a few hours to learn the basics of LISP.  Your AutoLISP posts reveal extreme unfamiliarity with it's syntax.  I suggest http://www.afralisp.net/autolisp/ as a place to start. Autolisp can be a real boon but forums are not a real good place to start learning.

Autolisp, for example, interprets the first item of a list as a function unless it is quoted to avoid direct evaluation.  AutoLISP lists do not use commas.

Architect, Registered NC, VA, SC, & GA.
0 Likes

john.uhden
Mentor
Mentor

Well, your structure is a little errant.  You can't...

(setq c (15.3,16.2))

You could create a list...

(setq c (list 15.3 16.2)) or (setq c '(15.3 16.2))

 

I don't have a problem with the zeroes, but the nils are certainly no good.

I guess you could break up the command...

(command "_.pline")

(if (vl-every 'numberp a)(command a))

(if (vl-every 'numberp b)(command b))

etc.

John F. Uhden

0 Likes

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

 

....

(command "pline" a b c d "")

 

suppose the values in setq c and setq d is zero or nil, then (command "pline" a b c d) will be error. so, i have to remove c and d (because its value is nil) from the list by checking its value and make the command as (command "pline" a b ""). so, how can i make this? .... 


Assuming constructing those things correctly as others have pointed out....

 

If  you can rely on what's in all those variables to always  be either a point list [a list of 2 or 3 numbers] or nil, and never anything else  [such as a single number -- your zero example -- or text, or something], then you could do this:

 

(command "_.pline"); leaves you in the command awaiting input

(if a (command a)); feed to PLINE only if it exists [for each]

(if b (command b))

(if c (command c))

(if d (command d))

(command ""); finish Polyline

 

If any of them ever contain something else, it will cause an error because it will be fed to the PLINE command but will not be acceptable input, or if it is, it will be something like text that is viable for designating an option, and then very likely the next input won't be appropriate for that option.  It would be possible to check not only whether those variables exist but also whether they represent points, with some additional code.

Kent Cooper, AIA
0 Likes

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

 

 ...suppose the values in setq c and setq d is zero or nil, then (command "pline" a b c d) will be error. so, i have to remove c and d (because its value is nil) f

 

 


What if either a or b is zero or nil? does it still continue to create the pline using just c and d??

IF the goal is to create a pline whenever there are at least two points.

 

(if (>= (length (setq lst (vl-remove-if 'null (list a b c d)))) 2)
	(progn
		(command "_pline" )
			(Foreach pt lst (command "_non" pt)
			  )
		(command "")
	  )
  )

 

roland.r71
Collaborator
Collaborator
Accepted solution

@Anonymous

So... where's the list where you wish to append or vl-remove items???

 

PLEASE try to use subjects/titles that relate to your question!

Your title refers to a completely different 'problem' Smiley Frustrated

 

Anyway,

 


@Kent1Cooper wrote:

@Anonymous wrote:

 

....

(command "pline" a b c d "")

 

suppose the values in setq c and setq d is zero or nil, then (command "pline" a b c d) will be error. so, i have to remove c and d (because its value is nil) from the list by checking its value and make the command as (command "pline" a b ""). so, how can i make this? .... 


Assuming constructing those things correctly as others have pointed out....

 

If  you can rely on what's in all those variables to always  be either a point list [a list of 2 or 3 numbers] or nil, and never anything else  [such as a single number -- your zero example -- or text, or something], then you could do this:

 

(command "_.pline"); leaves you in the command awaiting input

(if a (command a)); feed to PLINE only if it exists [for each]

(if b (command b))

(if c (command c))

(if d (command d))

(command ""); finish Polyline

 

If any of them ever contain something else, it will cause an error because it will be fed to the PLINE command but will not be acceptable input, or if it is, it will be something like text that is viable for designating an option, and then very likely the next input won't be appropriate for that option.  It would be possible to check not only whether those variables exist but also whether they represent points, with some additional code.


 

IF the above is true, you might even get away with:

(command "_pline" a b (if c c) (if d d) (if d nil))

 

Note: if not set, each if reads as nil. (ending the pline command) Any extra nil doesn't break the code (in my simple quick test anyway), but you do need 1 nil to end the pline if all variables are set, so therefor i added an extra (if d nil).

0 Likes

roland.r71
Collaborator
Collaborator
Accepted solution

That would be a much beter solution.

 

Although it would be even beter if the list with points where to be filled with all (valid) points needed.

With a minimum of 2 points, you could create a pline with as many points as you wish.

0 Likes

john.uhden
Mentor
Mentor
Accepted solution

@Kent1Cooper wrote, "If any of them ever contain something else, it will cause an error "

Please don't tell me you subscribe to the ActivistInvestor method of programming, i.e. having errors tell you that there are errors <splat>.

BTW, "any" is singular... ""If any of them ever contains ..."

 

BTW, (if a (command a)) might bite you in the butt...

Command: (setq a (list nil nil))
(nil nil)

Command: (defun is (x) (not (not x)))
IS

Command: (is nil) nil

Command: (is a) T

a may be a list, but its contents are nils.  That's why I used (vl-every 'numberp a)

John F. Uhden

0 Likes

roland.r71
Collaborator
Collaborator

@john.uhden wrote:

@Kent1Cooper wrote, "If any of them ever contain something else, it will cause an error "

Please don't tell me you subscribe to the ActivistInvestor method of programming, i.e. having errors tell you that there are errors <splat>.

BTW, "any" is singular... ""If any of them ever contains ..."

 

BTW, (if a (command a)) might bite you in the butt...

Command: (setq a (list nil nil))
(nil nil)

Command: (defun is (x) (not (not x)))
IS

Command: (is nil) nil

Command: (is a) T

a may be a list, but its contents are nils.  That's why I used (vl-every 'numberp a)


 

Hence the disclaimer, that a,b,c & d should (must) be either a valid point list or nil. Anything else = error.

It's not the pinnacle of lisp programming, but it works, with minimal effort.

IF you're absolutely sure 'a' will never be a list containing nils, then why check?

0 Likes

john.uhden
Mentor
Mentor

I'm not even sure I will wake up in the morning.

John F. Uhden

0 Likes

roland.r71
Collaborator
Collaborator

@john.uhden wrote:

I'm not even sure I will wake up in the morning.



But i'm 100% sure you have to pay your taxes, untill you don't wake up. Smiley LOL

0 Likes

john.uhden
Mentor
Mentor
@john.uhden wrote:

"I'm not even sure I will wake up in the morning."

@roland.r71 replied:
"But i'm 100% sure you have to pay your taxes, untill you don't wake up."

 

Yes, that's for sure.  A very sad circumstance.  :[

John F. Uhden

0 Likes

Type a product name