TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: RAIN CODE on May 08, 2012, 06:36:49 AM

Title: how to replace at item in a list
Post by: RAIN CODE on May 08, 2012, 06:36:49 AM

Hi,

 I have a problem here hope someone can help.
Example a list - (list 1 2 2 3 3 1 5 4 9 3) and I want to replace the 2nd item of the list with 6.
            results  (list 1 6 2 3 3 1 5 4 9 3)
Is there any preset Lisp function that can replace an item in a list. I used subst but it replace others too which I wanted it unchange.
I have written a lisp for it but the lisp is long and slow down the game when the main program call this subroutine Lisp too often.

Thanks
Title: Re: how to replace at item in a list
Post by: Lee Mac on May 08, 2012, 06:44:03 AM
http://lee-mac.com/substn.html (http://lee-mac.com/substn.html)
Title: Re: how to replace at item in a list
Post by: Tharwat on May 08, 2012, 07:34:13 AM
My version ..  :-)
Code - Auto/Visual Lisp: [Select]
  1. (defun _ReplaceItem (place item lst / lol i)
  2. ;;;             Tharwat 08. May 2012                  ;
  3. ;;; place = Place of item to be replaced              ;
  4. ;;; item  = the new item to take a place              ;
  5. ;;; lst   = list of items to implement the changes on ;
  6.   (if (and lst (> (length lst) place))
  7.     (progn
  8.       (setq i 0)
  9.       (repeat (length lst)
  10.         (setq lol (cons (if (eq place (1+ i))
  11.                           item
  12.                           (nth i lst)
  13.                         )
  14.                         lol
  15.                   )
  16.         )
  17.         (setq i (1+ i))
  18.       )
  19.     )
  20.   )
  21.   (reverse lol)
  22. )


usage of function ..

Code - Auto/Visual Lisp: [Select]
  1. (_ReplaceItem 2 6 (list 1 2 2 3 3 1 5 4 9 3))

another with Strings ...

Code - Auto/Visual Lisp: [Select]
  1. (_ReplaceItem 2 "AutoLISP" (list 1 2 2 3 3 1 5 4 9 3))
Title: Re: how to replace at item in a list
Post by: CAB on May 08, 2012, 10:59:19 AM
The Old topic  8-)
http://www.theswamp.org/index.php?topic=14170.0
Title: Re: how to replace at item in a list
Post by: RAIN CODE on May 25, 2012, 06:01:29 AM
many thanks guys for your help

you all are the Jedi master. I feel like a newbie when i read thru' the lisp here.

Title: Re: how to replace at item in a list
Post by: CAB on May 25, 2012, 07:35:04 AM
You're welcome.
Keep hanging out.  8-)
Title: Re: how to replace at item in a list
Post by: irneb on May 25, 2012, 09:56:11 AM
My version:
Code - Auto/Visual Lisp: [Select]
  1. (defun replace-n (new n lst / )
  2.   (mapcar '(lambda (a) (if (= (setq n (1- n)) -1) new a)) lst))
Title: Re: how to replace at item in a list
Post by: irneb on May 25, 2012, 02:44:39 PM
Or to use the CL (Common Lisp) replace method: http://alisp-ext.wikidot.com/auotolisp-replace

It could probably be done better / faster / more elegant. You might also like some of the others:
http://alisp-ext.wikidot.com/autolisp
Title: Re: how to replace at item in a list
Post by: pBe on May 27, 2012, 09:11:31 AM
just to be on the mix:

Code - Auto/Visual Lisp: [Select]
  1. (defun _rep  (lst i v / l)
  2.               (repeat i
  3.                     (setq l   (cons (Car lst) l)
  4.                           lst (cdr lst)))
  5.               (apply 'append (list (reverse l) (list v) (cdr lst))))

Title: Re: how to replace at item in a list
Post by: irneb on May 28, 2012, 06:10:35 AM
That's probably a lot more efficient than mine. How about this one as well:
Code - Auto/Visual Lisp: [Select]
  1. (defun replaceN  (new N lst / result)
  2.   (cond ((or (< N 0) (>= N (length lst))) lst)
  3.         ((< N (/ (length lst) 2))
  4.          (repeat n
  5.            (setq result (cons (car lst) result)
  6.                  lst    (cdr lst)))
  7.          (append (reverse result) (list new) (cdr lst)))
  8.         (t
  9.          (setq lst (reverse lst))
  10.          (repeat (- (length lst) n)
  11.            (setq result (cons (car lst) result)
  12.                  lst    (cdr lst)))
  13.          (append (reverse lst) (list new) (cdr result)))))