Author Topic: substitute nth in a list  (Read 1890 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
substitute nth in a list
« on: January 13, 2011, 09:54:34 AM »
In this code
i have a function that reads a txt file and makes a list of its contents.

then i have a function that searches that list for a specific word.

when that word is found im trying to get it to remove that word and replace it with another word without it changing its position in the list
i think im on the right track however its wiping out the whole list just for the word im trying to replace it with
can anyone guide me in the right direction on how i can replace an nth in a list without destroying the whole list

ill eventually have to write it back to a text file but i think i have that one covered with a foreach function

thanks for any help
Code: [Select]
(defun readfile (filename / f path line lst)
  (if (setq path (strcat (getvar "Dwgprefix")
                           (vl-Filename-Base (vl-Filename-Directory (getvar "Dwgprefix")))
                            " Dwg List.txt"
                   )
 )
    (cond
      ((setq f (open path "r"))
       (while (setq line (read-line f))
(setq lst (cons line lst))
       )
       (close f)
       (reverse lst)
      )
    )
  )
)

(if (setq lst (readfile path))
   (progn
   (setq avar (nth 0 lst))
     (defun wcmatch_first ( lst pattern flag / result )
       (setq pattern (if flag (strcase pattern) pattern))
       (vl-some
         '(lambda (str) (if (wcmatch str pattern) (setq result str)))
          (if flag (mapcar 'strcase lst) lst)
       )
       result
     )
   (progn
     (if (setq item (wcmatch_first lst (strcat "*" old_title "*") nil))  ; change this for search word
 (progn
  (setq o_var (vl-position item lst))
  (setq lst1 (vl-string-subst "hello" (nth o_var lst) (nth o_var lst)))
  )
     )
   )
   )
 (alert "no")
    (progn
;; if file doesnt exist
  ; (do other)
)
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: substitute nth in a list
« Reply #1 on: January 13, 2011, 10:00:59 AM »
;;  http://www.theswamp.org/index.php?topic=14170.msg170689#msg170689
Code: [Select]
;;  Evigeny
(defun replace (lst i itm)
  ;;(replase '(0 1 2 3 4 5 6) 3 "A")
  ;; =>
  ;; '(0 1 2 "A" 4 5 6)
  (mapcar
    (function
      (lambda (x)
        (if (zerop i)
          (progn (setq i (1- i)) itm)
          (progn (setq i (1- i)) x)
        )
      )
    )
    lst
  )
)

Code: [Select]
;; variation by CAB
(defun replace_nth (lst i itm)
  (setq i (1+ i))
  (mapcar '(lambda (x) (if (zerop (setq i (1- i))) itm x)) lst)
)


;;  http://www.theswamp.org/index.php?topic=30563.msg361742#msg361742
Code: [Select]
;;  VovKa
(defun SubstNth
   (NewItem Position InList /)
  (if InList
    (if (zerop Position)
      (cons NewItem (cdr InList))
      (cons (car InList) (SubstNth NewItem (1- Position) (cdr InList)))
    )
  )
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

andrew_nao

  • Guest
Re: substitute nth in a list
« Reply #2 on: January 13, 2011, 10:18:31 AM »
thanks CAB
im getting
error: bad argument type: numberp:

when i used the last two

andrew_nao

  • Guest
Re: substitute nth in a list
« Reply #3 on: January 13, 2011, 10:28:23 AM »
never mind CAB i figured it out

thanks again

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: substitute nth in a list
« Reply #4 on: January 13, 2011, 10:31:15 AM »
You're welcome 8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.