Looking in my old lisp items I found these:
;; CAB 10/18/2005
;; returns a list of pointers (base 0) of the
;; item found in the list
(defun getpos (itm lst / idn nlst)
(setq idx -1)
(while (< (setq idx (1+ idx)) (length lst))
(and (= itm (nth idx lst))
(setq nlst (cons idx nlst))))
(reverse nlst)
)
;; (print (GetPos 3 '(1 2 3 1 2 3 1 3 5)))
;; returns (2 5 7)
;; CAB 12/17/2009
;; returns a list of pointers (base 0) of the
;; item found in the list
(defun getpos (pat lst / i plst)
(setq i 0)
(mapcar
(function
(lambda (x)
(and (wcmatch x pat) (setq plst (cons i plst)))
(setq i (1+ i))))
lst)
(reverse plst))
(defun c:test()
(princ (getpos "*25" '("0425" "123" "345" "625")))
(princ)
)
And these:
;; CAB 05/22/2009
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++
;; Return a sub list starting at the nth position and
;; with all the remaining items
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++
(defun nth+ (lst idx)
(repeat idx
(setq lst (cdr lst))
)
lst
)
;; http://www.theswamp.org/index.php?topic=7269.0
;; CAB 10/15/2005
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++
;; Return a sub list starting at the nth position and
;; with the number of items specified by num 0= all remaining items
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++
(defun nth+ (lst idx num / newlst)
(and (or (numberp num) (setq num 0)) ; force all
(zerop num) ; if zero
(setq num (length lst)) ; all
)
(repeat num
(setq newlst (cons (nth idx lst) newlst)
idx (1+ idx))
)
(reverse (vl-remove nil newlst))
)
(defun c:test()
(print (nth+ '(1 2 3 4 5) 0 2)) ; |-> (1 2)
(print (nth+ '(1 2 3 4 5) 3 2)) ; |-> (4 5)
(print (nth+ '(1 2 3 4 5) 4 5)) ; |-> (5)
(print (nth+ '(1 2 3 4 5) 6 2)) ; |-> nil
(print (nth+ '(1 2 3 4 5) 2 0)) ; |-> (3 4 5)
(princ)
)
;; CAB 10/15/2005
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++
;; Return a sub list starting at the nth position and
;; with the number of items specified by num
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++
(defun nth+ (idx ; start position 0 = first item
lst ; list of items
num ; number of items to return 0= all remaining items
/ newlst)
(and (or (numberp num) ; catch non numbers
(setq num 0)) ; force all
(zerop num) ; if zero
(setq num (length lst)) ; all
)
(repeat num
(setq newlst (cons (nth idx lst) newlst)
idx (1+ idx))
)
(reverse (vl-remove nil newlst))
)
(defun c:test()
(print (nth+ 0 '(1 2 3 4 5) 2)) ; |-> (1 2)
(print (nth+ 3 '(1 2 3 4 5) 2)) ; |-> (4 5)
(print (nth+ 4 '(1 2 3 4 5) 5)) ; |-> (5)
(print (nth+ 6 '(1 2 3 4 5) 2)) ; |-> nil
(print (nth+ 2 '(1 2 3 4 5) 0)) ; |-> (3 4 5)
(princ)
)
;;=======================================================
;; David B
;; http://cadtutor.net/forum/showpost.php?p=218577&postcount=9
(defun sublst (l s n / tmp)
(repeat n (setq tmp (cons (nth s l) tmp))
(setq s (1+ s)))
(reverse tmp))
(setq l1 '(1 2 3 4 5 6 7))
(prin1 (sublst l1 2 4))
;;=======================================================
;; by ASMI extract nth position items
;; http://cadtutor.net/forum/showpost.php?p=218187&postcount=5
(defun Extract-List-Items(Lst Items)
(mapcar '(lambda(i)(nth i Lst))Items)
)
Command: (Extract-List-Items '(a b c d e f) '(0 4 5))
(A E F)