Author Topic: How to replace elements in a table ?  (Read 2554 times)

0 Members and 1 Guest are viewing this topic.

baitang36

  • Bull Frog
  • Posts: 213
How to replace elements in a table ?
« on: August 19, 2021, 03:10:29 AM »
Replace an element in the table, such as' (1 2 3 4 5 6)
Want to replace the third number with 8, that is, return '(1 2 8 4 5 6)

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: How to replace elements in a table ?
« Reply #1 on: August 19, 2021, 03:44:13 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun test ( / idx val inputList i outPutList )
  2.   (setq idx 3)
  3.   (setq val 8)
  4.   (setq inputList '(1 2 3 4 5 6))
  5.   (setq i 0)
  6.   (setq outPutList
  7.     (mapcar
  8.       '(lambda (itm)
  9.         (if (= idx (setq i (1+ i)))
  10.           val
  11.           itm
  12.         )
  13.       )
  14.       inputList
  15.     )  
  16.   )
  17.   outPutList
  18. )
  19. (test)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ronjonp

  • Needs a day job
  • Posts: 7526
Re: How to replace elements in a table ?
« Reply #2 on: August 19, 2021, 11:20:57 AM »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
Re: How to replace elements in a table ?
« Reply #3 on: August 19, 2021, 12:14:17 PM »
Replacement functions abound!

NOTE: If you own any of the ones below, feel free to post here and I'll add your name for credit. 3,5, & 8 look like my style. 2 looks like Evgeniy's style.


If you know the position of the item to be replaced before hand, you may want to checkout replace--3.

Code - Auto/Visual Lisp: [Select]
  1.  
  2.   (defun replace--1 (lst i itm)
  3.     ;;(replase '(0 1 2 3 4 5 6) 3 "A")
  4.     ;; =>
  5.     ;; '(0 1 2 "A" 4 5 6)
  6.     (mapcar
  7.       (function
  8.         (lambda (x)
  9.           (if (zerop i)
  10.             (progn (setq i (1- i)) itm)
  11.             (progn (setq i (1- i)) x)
  12.           ) ;_ if
  13.         ) ;_ lambda
  14.       ) ;_ function
  15.       lst
  16.     ) ;_ mapcar
  17.   ) ;_ defun
  18.  
  19.   (defun replace--2 (lst i itm)
  20.     ;;(replace--2 '(0 1 2 3 4 5 6) 3 "A")
  21.     ;; =>
  22.     ;; '(0 1 2 "A" 4 5 6)
  23.     (if lst
  24.       (if (> i 0)
  25.         (cons (car lst) (replace--2 (cdr lst) (1- i) itm))
  26.         (cons itm (cdr lst))
  27.       ) ;_ if
  28.     ) ;_ if
  29.   )
  30.  
  31.   (defun replace--3 ( pos new-item lst )
  32.     ;; (replace--3 2 3 '(1 2 4 4))
  33.     ;; =>
  34.     ;; (1 2 3 4)
  35.     (if (null lst)
  36.       nil
  37.       (cons
  38.         (if (eq pos 0) new-item (car lst))
  39.         (replace--3 (1- pos) new-item (cdr lst)))) )
  40.  
  41.   (defun replace--4 (lst i itm)
  42.     (setq i (1+ i))
  43.     (mapcar
  44.       '(lambda (x)
  45.         (if (zerop (setq i (1- i))) itm x)
  46.       )
  47.       lst
  48.     )
  49.   )
  50.  
  51.   (defun replace--5  (num lst elem / cont lth lst1)
  52.     (setq       cont 0
  53.         lth  (length lst))
  54.     (while (/= cont num)
  55.       (setq lst1 (append lst1 (list (nth cont lst)))
  56.           cont (1+ cont)))
  57.     (setq       lst1 (append lst1 (list elem))
  58.         cont (1+ cont))
  59.     (while (< cont lth)
  60.       (setq lst1 (append lst1 (list (nth cont lst)))
  61.           cont (1+ cont)))
  62.     lst1)
  63.  
  64.   (defun replace--6 (lst i itm / c)
  65.     ;;(replace--6 '(0 1 2 3 4 5 6) 3 "A")
  66.     ;; =>
  67.     ;; '(0 1 2 "A" 4 5 6)
  68.     (setq c -1)
  69.     (mapcar
  70.       (function cdr)
  71.       ((lambda (l)
  72.          (subst
  73.            (cons 0 itm)
  74.            (assoc i l)
  75.            l
  76.          ) ;_ subst
  77.        ) ;_ lambda
  78.         (mapcar (function (lambda (x) (cons (setq c (1+ c)) x))) lst)
  79.       )
  80.     ) ;_ mapcar
  81.   ) ;_ defun
  82.  
  83.   (defun replace--7 (lst i1 itm / tmp nlst)
  84.     (while (and (> i1 0)
  85.                 (< (length (setq nlst (cons (car lst) nlst))) i1))
  86.       (setq lst (cdr lst))
  87.       )
  88.     (setq nlst (cons itm nlst)
  89.           lst (cddr lst))
  90.     (while (or (setq tmp (car lst)) lst)
  91.       (setq nlst (cons tmp nlst)
  92.             lst (cdr lst))
  93.       )
  94.     (reverse nlst)
  95.   )
  96.  
  97. (defun replace--8 (a b x)
  98.   ;; (replace--8 3 30 '(1 2 (3 4) (5 6 3) (3)))
  99.   ;; =>
  100.   ;;   (1 2 (30 4) (5 6 30) (30))
  101.   (defun replacer-n (x)
  102.     (if (atom x)
  103.       (if (not (eq a x)) x b)
  104.       (cons (replacer-n (car x))
  105.             (replacer-n (cdr x)))))
  106.    (replacer-n x) )
  107.  
  108. ;;; Elapsed milliseconds / relative speed for 65536 iteration(s):
  109. ;;;
  110. ;;;     (REPLACE--2 LST 3 "A").....1094 / 2.20 <fastest>
  111. ;;;     (REPLACE--1 LST 3 "A").....1453 / 1.66
  112. ;;;     (REPLACE--7 LST 3 "A").....1515 / 1.59
  113. ;;;     (REPLACE--3 3 "A" LST).....1672 / 1.44
  114. ;;;     (REPLACE--5 3 LST "A").....1813 / 1.33
  115. ;;;     (REPLACE--8 3 30 LST)......1969 / 1.22
  116. ;;;     (REPLACE--4 LST 3 "A").....2000 / 1.20
  117. ;;;     (REPLACE--6 LST 3 "A").....2407 / 1.00 <slowest>
  118. ;;;  ---- Benchmark utility: In memory of Michael Puckett --
  119.  
  120.   (setq lst '(1 2 3 4 5 6 7 8 9))
  121.   (benchmark
  122.     '(
  123.       (replace--1 lst 3 "A")
  124.       (replace--2 lst 3 "A")
  125.       (replace--3 3 "A" lst)
  126.       (replace--4 lst 3 "A")
  127.       (replace--5 3 lst "A")
  128.       (replace--6 lst 3 "A")
  129.       (replace--7 lst 3 "A")
  130.       (replace--8 3 30 lst)
  131.       )
  132.     )
  133.   )

Here is another which I think is cool.
Code - Auto/Visual Lisp: [Select]
  1. ;| A library function
  2. ; Multiple changing one elements of a list
  3. ; On other.
  4. ; Function usables in all events when it is possible
  5. ; Check on correspondence, through "EQUAL"
  6. ; Writer Evgeniy Elpanov.
  7. ; ***********************************************************
  8. ; Arguments:
  9. ; lst - a list in which it is necessary to make changings
  10. ; lst-i - correspondences ((changed replacer)... ())
  11. ; ***********************************************************
  12. ; An example of a call
  13. ; (multi-subst '(1 2 3 4 5) '((1 "a") (3 "b")))
  14. ; Function will return ("a" 2 "b" 45)
  15. |;
  16. (defun multi-subst (lst lst-i)
  17.   (eval
  18.     (list
  19.       (function mapcar)
  20.       (list
  21.         (function function)
  22.         (list
  23.           (function lambda)
  24.           '(a)
  25.           (cons
  26.                (function cond)
  27.                   (reverse
  28.                     (cons
  29.                       '(t a)
  30.                       (mapcar
  31.                         (function (lambda (a)
  32.                            (list
  33.                              (list
  34.                                (function equal)
  35.                                (car a)
  36.                                'a
  37.                              ) ;_  list
  38.                              (cadr a)
  39.                            ) ;_  list
  40.                          ) ;_  lambda
  41.                          ) ;_ function
  42.                         lst-i
  43.                       ) ;_  mapcar
  44.                     ) ;_  cons
  45.                   ) ;_  reverse
  46.                 ) ; _ cons
  47.         ) ; _ cons
  48.       ) ; _ list
  49.       'lst
  50.     ) ; _ list
  51.     ) ; _ eval
  52. )

EDIT: Fixed multi-sub code to work properly.
« Last Edit: August 19, 2021, 02:11:55 PM by John Kaul (Se7en) »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: How to replace elements in a table ?
« Reply #4 on: August 19, 2021, 01:12:44 PM »
Here is another which I think is cool.
'(t a) is in the wrong place
had to be append'ed not cons'ed

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
Re: How to replace elements in a table ?
« Reply #5 on: August 19, 2021, 02:13:29 PM »
Ugh! I just took for granted that the code I had in my notes was correct but I must have been playing with the code. I don't have "the original" so I modified the code to work. Fixed code posting.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: How to replace elements in a table ?
« Reply #6 on: August 19, 2021, 03:00:07 PM »
the code is still here at theswamp, one of the first Evgeniy's posts :)
it also has a little drawback: (multi-subst (list 'a 'b 'c) (list (list 'a 1)))

baitang36

  • Bull Frog
  • Posts: 213
Re: How to replace elements in a table ?
« Reply #7 on: August 20, 2021, 04:23:53 AM »
http://www.lee-mac.com/substn.html
thank you.
(defun LM:SubstNth ( a n l )
    (if l
        (if (zerop n)
            (cons a (cdr l))
            (cons (car l) (LM:SubstNth a (1- n) (cdr l)))
        )
    )
)

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
Re: How to replace elements in a table ?
« Reply #8 on: August 20, 2021, 11:51:39 AM »
the code is still here at theswamp, one of the first Evgeniy's posts :)
it also has a little drawback: (multi-subst (list 'a 'b 'c) (list (list 'a 1)))

That's a little..."specific" of a requirement (wouldn't most "library replace functions fail with that requirement") isn't it though? I mean, out of the above "replace--8" has the ability to operate--properly--on nested tree structures but how often will the case arise when your substitutions are also in a nested tree structure as well? -i.e. in your program, hopefully you would just have the values at hand and then preform the substitution on the tree.

That is to say, I can see this operation being more useful in more cases than not. I see this method as a nice-ish balance of usefulness and speed.
Code - Auto/Visual Lisp: [Select]
  1. (defun replace--8 (a b x)
  2.   ;; (replace--8 3 30 '(1 2 (3 4) (5 6 ((3))) (3)))
  3.   ;; =>
  4.   ;;   (1 2 (30 4) (5 6 ((30))) (30))
  5.   ;;
  6.   ;; (replace--8 3 30 '(1 2 (3 . 4) (5 6 ((3 . 2))) (3)))
  7.   ;; =>
  8.   ;; (1 2 (30 . 4) (5 6 ((30 . 2))) (30))
  9.   (defun replacer-n (x)
  10.     (if (atom x)
  11.       (if (not (eq a x)) x b)
  12.       (cons (replacer-n (car x))
  13.             (replacer-n (cdr x)))))
  14.    (replacer-n x) )

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: How to replace elements in a table ?
« Reply #9 on: August 20, 2021, 06:21:28 PM »
on nested tree structures
my remark has nothing to do with "nested tree structures"
Evgeniy's code substitute only numbers and strings but not symbols and lists
Code: [Select]
_$ (multi-subst (list 'a 'b 'c) (list (list 'a 1)))
(1 1 1) - bad
_$ (multi-subst (list "a" "b" "c") (list (list "a" 1)))
(1 "b" "c") - good

i think changing line 35 to
Code: [Select]
(list 'quote (car a)) will fix that
Code: [Select]
_$ (multi-subst '(a ("b") "c") '((a 1) (("b") 2) ("c" 3)))
(1 2 3) - good

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
Re: How to replace elements in a table ?
« Reply #10 on: August 20, 2021, 06:30:57 PM »
Ah! ...well, I went down the wrong path didn't I?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: How to replace elements in a table ?
« Reply #11 on: August 20, 2021, 06:36:22 PM »
Ah! ...well, I went down the wrong path didn't I?
not wrong but different :)

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: How to replace elements in a table ?
« Reply #12 on: August 20, 2021, 07:07:54 PM »
almost forgot ;)
Code: [Select]
(replace--8 nil 2 '(1 nil (3 4) (5 6 ((3))) (3)))

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
Re: How to replace elements in a table ?
« Reply #13 on: August 20, 2021, 08:11:04 PM »
almost forgot ;)
Code: [Select]
(replace--8 nil 2 '(1 nil (3 4) (5 6 ((3))) (3)))
Oh well yeah of course that will give some really fun results. ...that’s a feature *wink*.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: How to replace elements in a table ?
« Reply #14 on: August 22, 2021, 04:39:31 AM »
No recursion:
Code: [Select]
(defun ALE_List_SubstNth (NthPos NewItm In_Lst InRLst / LstLng OldItm)
; Marc'Antonio Alessi - ; Version 2.02 - 15/02/2008 > old name: ALE_SubstNth
  (cond
    ( (null In_Lst)                                nil )
    ( (zerop NthPos)        (cons NewItm (cdr In_Lst)) )
    ( (<= (setq LstLng (length In_Lst)) NthPos) In_Lst )
    ( (zerop (setq LstLng (- LstLng (1+ NthPos))))
      (append (reverse (cdr InRLst)) (list NewItm))
    )
    ( T
      (setq OldItm (nth NthPos In_Lst))
      (while (/= NthPos (length (setq InRLst (cdr (member OldItm InRLst))))))
      (while (/= LstLng (length (setq In_Lst (cdr (member OldItm In_Lst))))))
      (append (reverse InRLst) (cons NewItm In_Lst))
    )
  )
)

(defun ALE_List_SubstNth_NoRmv  (NthPos NewItm In_Lst / LstLng SttLst)
; Marc'Antonio Alessi - 2005
  (cond
    ( (> (/ (setq LstLng (length In_Lst)) 2) NthPos)
      (repeat NthPos
        (setq
          SttLst (cons (car In_Lst) SttLst)
          In_Lst (cdr In_Lst)
        )
      )
      (append (reverse SttLst) (cons NewItm (cdr In_Lst)))
    )
    ( (> (1+ NthPos) LstLng) In_Lst )
    ( T
      (setq In_Lst (reverse In_Lst))
      (repeat (1- (- LstLng NthPos))
        (setq
          SttLst (cons (car In_Lst) SttLst)
          In_Lst (cdr In_Lst)
        )
      )
      (append (reverse (cdr In_Lst)) (cons NewItm SttLst))
    )
  )
)
Code: [Select]
(benchmark
    '(
      (replace--1 lst 3 "A")
      (replace--2 lst 3 "A")
      (ALE_List_SubstNth 3 "A" lst (reverse Lst))
      (ALE_List_SubstNth_NoRmv 3 "A" lst )
      (LM:SubstNth "A" 3 lst)
      (LM:SubstNth "A" 3 lst)
      (ALE_List_SubstNth_NoRmv 3 "A" lst )
      (ALE_List_SubstNth 3 "A" lst (reverse Lst))
      (replace--2 lst 3 "A")
      (replace--1 lst 3 "A")
    )
)
    (REPLACE--2 LST 3 "A").......................1734 / 1.45 <fastest>
    (LM:SUBSTNTH "A" 3 LST)......................1735 / 1.45
    (LM:SUBSTNTH "A" 3 LST)......................1765 / 1.42
    (REPLACE--2 LST 3 "A").......................1766 / 1.42
    (ALE_LIST_SUBSTNTH_NORMV 3 "A" LST)..........1844 / 1.36
    (ALE_LIST_SUBSTNTH_NORMV 3 "A" LST)..........1985 / 1.27
    (ALE_LIST_SUBSTNTH 3 "A" LST (REVERS...).....2187 / 1.15
    (ALE_LIST_SUBSTNTH 3 "A" LST (REVERS...).....2219 / 1.13
    (REPLACE--1 LST 3 "A").......................2500 / 1.01
    (REPLACE--1 LST 3 "A").......................2515 / 1 <slowest>

--- Benchmark utility: In memory of Michael Puckett ---