Author Topic: How do I edit an item in a list with subst?  (Read 2153 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1105
How do I edit an item in a list with subst?
« 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:

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How do I edit an item in a list with subst?
« Reply #1 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")


dubb

  • Swamp Rat
  • Posts: 1105
Re: How do I edit an item in a list with subst?
« Reply #2 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")

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How do I edit an item in a list with subst?
« Reply #3 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"))

dubb

  • Swamp Rat
  • Posts: 1105
Re: How do I edit an item in a list with subst?
« Reply #4 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"))