saim Posted January 17, 2019 Share Posted January 17, 2019 (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 January 17, 2019 by saim found a way around Quote Link to comment Share on other sites More sharing options...
rlx Posted January 17, 2019 Share Posted January 17, 2019 (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) ) 1 Quote Link to comment Share on other sites More sharing options...
saim Posted January 17, 2019 Author Share Posted January 17, 2019 Hahah! Of course! Didn't realize the variable name was a function... I really need to use some more customized names... Thank you! Quote Link to comment Share on other sites More sharing options...
rlx Posted January 17, 2019 Share Posted January 17, 2019 (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 January 17, 2019 by rlx 1 Quote Link to comment Share on other sites More sharing options...
saim Posted January 18, 2019 Author Share Posted January 18, 2019 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? Quote Link to comment Share on other sites More sharing options...
rlx Posted January 18, 2019 Share Posted January 18, 2019 Visual lisp refenrence is a start http://help.autodesk.com/view/ACD/2016/ENU/?guid=GUID-4CEE5072-8817-4920-8A2D-7060F5E16547 Also sites like Lee Mac's site http://www.lee-mac.com/index.html , or Alfra Lisp are excellent sites https://www.afralisp.net/index.php 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.