count......separately

caduser

Usenet Archive
Joined
Jan 1, 2000
Messages
51
Reaction score
0
(setq blk '("A1" "A2" "B3" "C2" "B1" "A3" "C3" "B2" "C1")) how to count A B and C separately
 
separate them into separate lists, then sort each list in turn

(defun test( / entry varnamelist varname testlist)

;test list
(setq testlist '("A1" "A2" "B3" "C2" "B1" "A3" "C3" "B2" "C1"))

(foreach entry testlist
(if(boundp(setq varname(read(strcat"List"(substr entry 1 1 ))))) (set varname (cons entry (eval varname))) (set varname (list entry )) ) (if(not(member varname varnamelist))(setq varnamelist(cons varname varnamelist)))
)

(foreach varname varnamelist
;here's where you can sort each list in turn
;i'm just printing the contents for this sample
(print (eval varname)))

;you'd need to do something like this to clear the lists ;after you're done with them -= to "localize" the variables ;that are created on the fly depending on actual sorting list

;clear lists to localize on the fly variables
(if(and varnamelist
(listp varnamelist))
(foreach entry varnamelist
(set entry nil))
)
;clear vars
;these could just be localized in the parameter list of the function definition above ( / varnamelist varname) (setq varnamelist nil varname nil) (princ)
)

;hope that helps
;there's no error checking - if your list diverged from the simple pattern you give as an example, results may not be what you expect. You really need to know what possible variances your list can have to allow for any possible occurances.
 
Give this a try:

;;; Returns a list containing number of like items in a list. ;;; Evaluates first character in a string as a like item. ;;; (count_item '("A1" "A2" "B3" "C2" "B1" "A3" "C3" "B2" "C1")) ;;; (("A" . 3) ("C" . 3) ("B" . 3))

(defun count_item (alist / clist)
(mapcar '(lambda (x)
(setq clist
(cons
(cons (substr x 1 1)(length (keep_item alist x)))
clist)))
(remove_like alist))
(reverse clist)
)

;;;Based on Doug Broad's
;;;Recursive example.
(defun remove_like (lst)
(cond
((null lst) nil)
((cons (car lst)
(remove_like (remove_item lst))))
)
)

;;; Create new list of one set of like
;;; items removed from origianl list.
(defun remove_item (lst / nlist)
(foreach item lst
(and (not (eq (substr (car lst) 1 1)(substr item 1 1))) (setq nlist (cons item nlist))) ) nlist ) ;;; Create new list of like items from ;;; Original list (defun keep_item (lst comp / nlist) (foreach item lst (and (eq (substr comp 1 1)(substr item 1 1)) (setq nlist (cons item nlist))) ) nlist )
 
based on his(her?) earlier posts (in a diff thread) I thought they needed to keep the items so as to sort each list ("A1" "A2" "A3")("B1" "B2" ...etc rather than count the number of entries

nice batch of functions though (as always, coming from you!)
 

Latest threads

Back
Top