nila_joy Posted November 18, 2011 Posted November 18, 2011 Hi all... I have a list ("BLKCIR" "BLKCIR" "BLKCIR" "BLOCK1" "BLOCK1" "BLOCK1" "BLOCK1" "BLOCK1") I want count the numbers of same block, means if I have user input "BLOCK1... and It will be ( "BLOCK1" "BLOCK1" "BLOCK1" "BLOCK1" "BLOCK1")") I wrote a routine, but surely did some mistake (defun duplc () (setq name_lis '(..........) (setq lista '()) (setq listb '()) (setq blkinput(strcase (getstring "\nPlease Enter a Block Name :- "))) (setq count 0 len (length name_lis)) (While (< count len) (setq listc (nth count name_lis)) (if (= (member blkinput listc) nil) (setq lista (append lista (list blkinput))) ) ;if (setq listb (append listb (list lista))) (setq count (1+ count)) ) ; while listb ) ; defun I am a beginner in LISP.. this is for your kind info.. Quote
Lee Mac Posted November 18, 2011 Posted November 18, 2011 Using Visual LISP: (vl-remove-if-not '(lambda ( x ) (eq "BLOCK1" x)) '("BLKCIR" "BLKCIR" "BLKCIR" "BLOCK1" "BLOCK1" "BLOCK1" "BLOCK1" "BLOCK1") ) In Vanilla: (apply 'append (mapcar '(lambda ( x ) (if (eq "BLOCK1" x) (list x))) '("BLKCIR" "BLKCIR" "BLKCIR" "BLOCK1" "BLOCK1" "BLOCK1" "BLOCK1" "BLOCK1") ) ) BTW, did you resolve your last thread? http://www.cadtutor.net/forum/showthread.php?64380-any-other-function&p=439278 Quote
ketxu Posted November 19, 2011 Posted November 19, 2011 Or maybe like : (defun count (lst / old res) (foreach item lst (if (setq old (assoc item Res)) (setq Res (subst (cons item (1+ (cdr old))) old Res)) (setq Res (append Res (list (cons item 1)))) ) )) Or : (defun count (lst / ret) (while lst (setq ret (append Ret (list (cons (car lst)(- (length lst) (length (setq lst (vl-remove (car lst) lst)))))))) ) ret ) Quote
Lee Mac Posted November 19, 2011 Posted November 19, 2011 Another few: http://lee-mac.com/uniqueduplicate.html See 'List Occurrences' function. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.