Jump to content

Reverse function is not working


hei

Recommended Posts

Hello people.

 

So here I am, trying to create a lisp command, and already stuck at the very first part of it (Yes, Im kinda new with lisp and anything related with programming too)

 

Anyways, here's the code:

 

(defun c:test ( / pl ent chequeo vert)

(prompt "\nSeleccione objeto")
(setq pl (entget (car (entsel))))

(foreach ent pl
	(setq chequeo (car ent))
	(if (= chequeo 10)
	(setq vert (cons (cdr ent) vert))
	)
)
(reverse vert)
(print vert)(princ)
)

 

So I have noticed that the new list I am creating it has the items ordered in the wrong way that I want, and then I found the REVERSE function that fixes it for me, except that nothing happens at all. Whether I put REVERSE or not in the command it seems that the list remains unchanged, so I am not sure if I did something wrong here or I am using the REVERSE function wrong.

 

Any help is appreciated

 

Thanks in advance

Link to comment
Share on other sites

(defun c:test ( / plenx dxf chequeo vert )

 (setq plenx (entget (car (entsel "\nSeleccione objeto"))))

 (foreach dxf plenx
   (setq chequeo (car dxf))
   (if (= chequeo 10)
     (setq vert (cons (cdr dxf) vert))
   )
 )
 [color=red](setq vert [/color][color=blue](reverse vert)[/color][color=red])[/color]
 (print vert)
 (princ)
)

HTH, M.R.

Link to comment
Share on other sites

Lol, it seems to be kinda obvious now. Thanks a lot, it is working now!

 

But it makes me wonder... I have seen other codes where they don't have the setq before hand, now I am confused about when I am supposed to use setq before the command or when it is not needed :(

Link to comment
Share on other sites

Prompt function is not required as you give your message by Entsel too:-

(setq pl (entget (car (entsel "\nSeleccione objeto"))))

There are different methods of performing this operation such as :-

 

Foreach

(foreach l pl
 (if (= (car l) 10)
   (setq v (cons (cdr l) v))
 )
)
(setq v (reverse v))

 

Mapcar

(vl-remove nil
	   (mapcar '(lambda (x)
		      (if (eq (car x) 10)
			(cdr x)
		      )
		    )
		   pl
	   )
)

 

Vl-remove-if-not

(mapcar	'cdr
	(vl-remove-if-not
	  '(lambda (x)
	     (eq (car x) 10)
	   )
	  pl
	)
)

Link to comment
Share on other sites

FWIW, here is a performance comparison of several 'massoc' functions.

 

Excellent comparison Lee sir... :thumbsup:

 

My Function Comparison :-

   (PROGN (FOREACH L PL (IF (= (CAR L) ...).....1669 / 1.34 <fastest>
   (VL-REMOVE nil (MAPCAR (QUOTE (LAMBD...).....1903 / 1.17
   (MAPCAR (QUOTE CDR) (VL-REMOVE-IF-NO...).....2231 / 1 <slowest>

Link to comment
Share on other sites

I have seen other codes where they don't have the setq before hand, now I am confused about when I am supposed to use setq before the command or when it is not needed :(

 

To change the value of vert it's value must be reset somehow, but using setq is not always needed. You can use the output of a function without saving it to a value. Maybe this is what you're thinking of?

(defun c:test ( / pl ent chequeo vert)

(prompt "\nSeleccione objeto")
(setq pl (entget (car (entsel))))

(foreach ent pl
	(setq chequeo (car ent))
	(if (= chequeo 10)
	(setq vert (cons (cdr ent) vert))
	)
)
[color="blue"][b](reverse vert)[/b][/color]
)

Link to comment
Share on other sites

FWIW, here is a performance comparison of several 'massoc' functions.

 

Woah, the most efficient one seems to be calling itself as a function? How is that possible? It seems it is called as recursive, but I tried to search material about it, but none seems to be understandable by me lol.

 

Will look into it further when I finish what I am trying to accomplish here (in an attempt to make it more efficient too)

 

Anyways, another problem has came up, so Ill just post here in hope that someone might be able to help me without having to make a new thread:

 

Currently I am having with the following code:

 

(setq y-aux (mapcar 'cadr vert))
(print y-aux)(princ)
(setq n 0)
(repeat (- n-veces 1)
	(setq h-aux (- (min (nth n y-aux) (nth (+ n 1) y-aux)) (nth n y-fund)))
	(print h-aux)(princ)
	(set h-fund (cons h-aux h-fund))
	(print "checkpoint")(princ)
	(setq n (+ n 1))
)

 

And this is what Autocad is returning me:

 

(7.99292 8.07037 8.15563 8.25114 8.35942 8.48295 8.62478 8.78742 8.97419 9.1877 9.4312 9.70793 10.0208 10.3739 10.7689 11.1954 11.6348 12.0677 12.4751 12.8378 13.137 13.3557 13.486 13.535 13.5121 13.4241 13.2795 13.0853 12.8488 12.5772 )
1.57056 ; error: bad argument type: symbolp nil

 

I have added the print functions as a way to see where I am messing up, and from what it is returning to me, it seems that the following code seems to be at fault (considering that it is printing y-aux and h-fund correctly)

 

(set h-fund (cons h-aux h-fund))

 

Any ideas why? I have done the exact same function like three times previously (but with different variables) and they all worked fine except for this one.

 

Thanks in advance!

Link to comment
Share on other sites

The error is here:

(set h-fund (cons h-aux h-fund))

Unlike setq, the set function will evaluate both the symbol & value arguments (setq only evaluates the value argument), thereby evaluating the symbol h-fund which evidently yields a value of nil.

 

This should instead be either:

(setq h-fund (cons h-aux h-fund))

Or:

(set 'h-fund (cons h-aux h-fund))

Link to comment
Share on other sites

Ahhh thanks a lot!

 

That was a very stupid mistake and I was unable to see.... No wonder it didnt work this time although I have already done something similar like three times already

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...