count......separately

Discussion in 'AutoCAD' started by caduser, Jul 10, 2003.

  1. caduser

    caduser Guest

    (setq blk '("A1" "A2" "B3" "C2" "B1" "A3" "C3" "B2" "C1"))
    how to count A B and C separately
     
    caduser, Jul 10, 2003
    #1
  2. caduser

    Mark Propst Guest

    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.
     
    Mark Propst, Jul 10, 2003
    #2
  3. 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
    )

    --
    Ken Alexander
    Acad2000
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Jul 10, 2003
    #3
  4. caduser

    Mark Propst Guest

    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!)
     
    Mark Propst, Jul 10, 2003
    #4
  5. Gottcha.....and thanks, BTW

    --
    Ken Alexander
    Acad2000
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Jul 10, 2003
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.