TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: dubb on October 23, 2018, 12:23:22 PM

Title: How do I edit an item in a list with subst?
Post by: dubb on October 23, 2018, 12:23:22 PM
I'm having a bit of a misunderstanding on how this works.
Code: [Select]
(setq lst '("A" "B" "C"))
(subst '("1") '("A") lst)

Autodesk website shows an example of how it works but I'm not getting the results I need.
https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-AutoLISP/files/GUID-25214E69-090A-45C3-8210-6D9801255E44-htm.html
 :uglystupid2:
Title: Re: How do I edit an item in a list with subst?
Post by: Lee Mac on October 23, 2018, 12:26:57 PM
For example:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq lst '("A" "B" "C"))
  2. ("A" "B" "C")
  3. _$ (subst "1" "B" lst)
  4. ("A" "1" "C")

Be aware that subst will replace all occurrences of item in a list:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq lst '("A" "B" "C" "B" "C"))
  2. ("A" "B" "C" "B" "C")
  3. _$ (subst "1" "B" lst)
  4. ("A" "1" "C" "1" "C")

Title: Re: How do I edit an item in a list with subst?
Post by: dubb on October 23, 2018, 12:30:16 PM
Wow! That's it! So I missed it by treating each subst item as a list instead of a string.
For example:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq lst '("A" "B" "C"))
  2. ("A" "B" "C")
  3. _$ (subst "1" "B" lst)
  4. ("A" "1" "C")

Be aware that subst will replace all occurrences of item in a list:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq lst '("A" "B" "C" "B" "C"))
  2. ("A" "B" "C" "B" "C")
  3. _$ (subst "1" "B" lst)
  4. ("A" "1" "C" "1" "C")
Title: Re: How do I edit an item in a list with subst?
Post by: Lee Mac on October 23, 2018, 12:31:30 PM
Wow! That's it! So I missed it by treating each subst item as a list instead of a string.

Indeed - though, the subst item can be a list if that is what your list contains, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq lst '(("A") ("B") ("C")))
  2. (("A") ("B") ("C"))
  3. _$ (subst '("1") '("B") lst)
  4. (("A") ("1") ("C"))
Title: Re: How do I edit an item in a list with subst?
Post by: dubb on October 23, 2018, 12:38:56 PM
Awesome! I love it. Thank you for enlightening me.

Wow! That's it! So I missed it by treating each subst item as a list instead of a string.

Indeed - though, the subst item can be a list if that is what your list contains, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq lst '(("A") ("B") ("C")))
  2. (("A") ("B") ("C"))
  3. _$ (subst '("1") '("B") lst)
  4. (("A") ("1") ("C"))