Jump to content

Recommended Posts

Posted (edited)

There's probably an easier way to do it, but the hard way SHOULD work...

I want to replace a member of a list, given that I know the list, the member and the replacement.

Here's my attempt:

(defun replace(list toreplace newincome / tl1 tl2 ml returnlist)
	(setq tl1 (reverse list)) ; reverses order
	(setq tl1 (member toreplace tl1)) ; erases what is after the member
	(setq tl1 (cdr tl1)) ; erases member
	(setq tl1 (reverse tl1)) ; returns to original order
	(print tl1) ; debug
	(setq tl2 (member toreplace list)) ; erases what is before the member
	(setq tl2 (cdr tl2)) ; erases member
	(print tl2) ; debug
	(setq ml (list newincome)) ; the new member, in list format
	(print ml) ; debug
	(setq returnlist (append tl1 ml tl2)) ; makes the new list
returnlist
)

and the code made to test it:

(defun c:tstrep( / listoriginal membertoreplace newincome resultado tl3)
    (setq listoriginal (list 0 1 2 3 4 5 6))
    (setq membertoreplace 3)
    (setq newincome 8)
    (setq resultado (replace listoriginal membertoreplace newincome))
    (print resultado)
)

At the point that I try to make a list just with the new member, it returns a "bad function" error.
I do not believe there is any mispelling in the code (I copy-pasted a lot), so what's the error?

 

Edit: Found a way around... I made this:

(defun replace(list toreplace newincome / tl1 tl2 returnlist)
	(setq tl1 (reverse list)) ; troca a ordem
	(setq tl1 (member toreplace tl1)) ; elimina o que vem depois
	(setq tl1 (cdr tl1)) ; elimina o elemento
	(setq tl1 (reverse tl1)) ; volta pra ordem normal
	(print tl1) ; debug
	(setq tl2 (member toreplace list)) ; elimina o que vem antes
	(setq tl2 (cdr tl2)) ; elimina o elemento
	(print tl2) ; debug
	(setq returnlist (cons newincome tl2)) ; adiciona o elemento
	(setq returnlist (append tl1 returnlist)) ; adiciona o elemento
returnlist
)

But I'm still curious on why the first method didn't work.
 

Edited by saim
found a way around
Posted


(defun replace ( lst toreplace newincome / tl1 tl2 ml returnlist)
    (setq tl1 (reverse lst)) ; reverses order
    (setq tl1 (member toreplace tl1)) ; erases what is after the member
    (setq tl1 (cdr tl1)) ; erases member
    (setq tl1 (reverse tl1)) ; returns to original order
    (print tl1) ; debug
    (setq tl2 (member toreplace lst)) ; erases what is before the member
    (setq tl2 (cdr tl2)) ; erases member
    (print tl2) ; debug
    (setq ml (list newincome)) ; the new member, in list format
    (print ml) ; debug
    (setq returnlist (append tl1 ml tl2)) ; makes the new list
returnlist
)

(defun c:tstrep( / listoriginal membertoreplace newincome resultado tl3)
    (setq listoriginal (list 0 1 2 3 4 5 6))
    (setq membertoreplace 3)
    (setq newincome 8)
    (setq resultado (replace listoriginal membertoreplace newincome))
    (print resultado)
)

  • Like 1
Posted

Hahah!
Of course!

Didn't realize the variable name was a function...

I really need to use some more customized names...

 

Thank you!

Posted (edited)

nos problemos 🙂

 

I allways use 'check code in editor' in the vlisp editor and this would have found the error. Also the fact that 'list' turns blue is a clue. But of course this one was a little obvious. But hey, that's what learning is all about 🤓. Check the 'subst' function...

 

And if you want only subst the first item look at this one : http://www.lee-mac.com/substonce.html

Edited by rlx
  • Like 1
Posted

Hey, it DOES turn blue!
Believe it or not, I did use the vlisp editor in this case (because of the parenthesis - I needed a tool to show where I had forgotten to close them). But since I rarelly use it, I did not pay attention to the color coding.

 

...

And also had to search for another tool, for the editor doesn't show which parenthesis closes which (I used the "codeanywhere" site).

 

 

Man, "subst" does exactly what I meant to do! I mean, not EXACTLY, but the result is the same (there'll be only one element of each kind on my list).

Where can I find a list with all list functions?

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...