I thought it might be useful to create a set of functions for manipulating Lists.
So I just wrote these functions.
Any ideas for other functions?
P=
Code:
;___________________________________________________________________________________________________________|
;
; Basic List Manipulation Functions
; Written By: Peter Jamtgaard Copyright 2015
;___________________________________________________________________________________________________________|
(defun C:ListFunctionTest (/ lstOfValues)
(and
(setq lstOfValues (list "A" "B" "C" "D" 1 2 3 4))
(setq lstOfValues (listinsert 4 lstOfValues 0))
(princ "\n")
(princ lstOfValues)
(setq lstOfValues (listremove 4 lstOfValues))
(princ "\n")
(princ lstOfValues)
(setq lstOfValues (listmoveup 3 lstOfValues))
(princ "\n")
(princ lstOfValues)
(setq lstOfValues (listmovedown 2 lstOfValues))
(princ "\n")
(princ lstOfValues)
(setq lstOfValues (listreplace 0 lstOfValues "Z"))
(princ "\n")
(princ lstOfValues)
)
)
; Function to add index number to front of value or sublist
;___________________________________________________________________________________________________________|
(defun ListIndex (lstOfSublists / intCount )
(setq intCount -1)
(mapcar '(lambda (X)(cons (setq intCount (1+ intCount)) X)) lstOfSublists)
)
; Function to insert a Value into a list (lstPrimary) at a position (intItem) - Zero (0) is the first item
;___________________________________________________________________________________________________________|
(defun ListInsert (intItem lstPrimary Value / lstIndexed lstSublist)
(if (and
(or intItem
(setq intItem (length lstPrimary))
)
(setq lstIndexed (ListIndex lstPrimary))
(setq lstSublist (cons (- intItem 0.5) Value))
(setq lstIndexed (reverse (cons lstSublist (reverse lstIndexed))))
(setq lstIndexed (SortListOfSublistsByItem lstIndexed 0))
)
(mapcar 'cdr lstIndexed)
lstPrimary
)
)
; Function to move a Value down in a list (lstPrimary) at a position (intItem) - Zero (0) is the first item
;___________________________________________________________________________________________________________|
(defun ListMoveDown (intItem lstPrimary / lstIndexed)
(if (and
(< -1 intItem (- (length lstPrimary) 2))
(setq lstIndexed (ListIndex lstPrimary))
(setq Value (cdr (assoc intItem lstIndexed)))
(setq lstIndexed (subst (cons (+ intItem 1.5) Value) (assoc intItem lstIndexed) lstIndexed))
(setq lstIndexed (SortListOfSublistsByItem lstIndexed 0))
)
(mapcar 'cdr lstIndexed)
lstPrimary
)
)
; Function to move a Value up in a list (lstPrimary) at a position (intItem) - Zero (0) is the first item
;___________________________________________________________________________________________________________|
(defun ListMoveUp (intItem lstPrimary / lstIndexed)
(if (and
(< 0 intItem (1- (length lstPrimary)))
(setq lstIndexed (ListIndex lstPrimary))
(setq Value (cdr (assoc intItem lstIndexed)))
(setq lstIndexed (subst (cons (- intItem 1.5) Value) (assoc intItem lstIndexed) lstIndexed))
(setq lstIndexed (SortListOfSublistsByItem lstIndexed 0))
)
(mapcar 'cdr lstIndexed)
lstPrimary
)
)
; Function to remove a Value from a list (lstPrimary) at a position (intItem) - Zero (0) is the first item
;___________________________________________________________________________________________________________|
(defun ListRemove (intItem lstPrimary / lstIndexed lstSublist)
(if (and
(setq lstIndexed (ListIndex lstPrimary))
(setq lstSublist (assoc intItem lstIndexed))
(setq lstIndexed (vl-remove lstSublist lstIndexed))
)
(mapcar 'cdr lstIndexed)
lstPrimary
)
)
; Function to replace a Value down in a list (lstPrimary) at a position (intItem) - Zero (0) is the first item
;___________________________________________________________________________________________________________|
(defun ListReplace (intItem lstPrimary Value / lstIndexed lstSublist)
(if (and
(setq lstIndexed (ListIndex lstPrimary))
(setq lstSublist (assoc intItem lstIndexed))
(setq lstIndexed (subst (cons intItem Value) lstSublist lstIndexed))
)
(mapcar 'cdr lstIndexed)
lstPrimary
)
)
; General Function for sorting a list of sublists into ascending order of a specified item in the sublists:
;___________________________________________________________________________________________________________|
(defun sortListofSublistsbyItem (lstOfSublists intItem)
(vl-sort lstOfSublists '(lambda (X Y) (< (nth intItem X) (nth intItem Y))))
)
(vl-load-com)