Author Topic: List Same Data  (Read 1719 times)

0 Members and 1 Guest are viewing this topic.

udaaf

  • Guest
List Same Data
« on: November 25, 2015, 03:47:25 PM »
How to list same data become one list.
Here's the data :

Code: [Select]
("Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Zanirman" "Zanirman" "Zanirman" "Indonesian Drafting School")

I want he result like this :
Code: [Select]
(("Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri")( "Zanirman" "Zanirman" "Zanirman") ("Indonesian Drafting School"))

Thanks
 

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: List Same Data
« Reply #1 on: November 25, 2015, 04:10:58 PM »
Here's a simple method:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( l / r y )
  2.     (foreach x l
  3.         (if (setq y (assoc x r))
  4.             (setq r (subst (cons x y) y r))
  5.             (setq r (cons  (list x) r))
  6.         )
  7.     )
  8.     (reverse r)
  9. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq lst '("Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Zanirman" "Zanirman" "Zanirman" "Indonesian Drafting School"))
  2. ("Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Zanirman" "Zanirman" "Zanirman" "Indonesian Drafting School")
  3. _$ (foo lst)
  4. (("Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri" "Afri") ("Zanirman" "Zanirman" "Zanirman") ("Indonesian Drafting School"))

Here's another:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo2 ( l / a b x )
  2.     (if (setq x (car l))
  3.         (progn
  4.             (foreach y (cdr l)
  5.                 (if (= x y)
  6.                     (setq a (cons y a))
  7.                     (setq b (cons y b))
  8.                 )
  9.             )
  10.             (cons (cons x a) (foo2 (reverse b)))
  11.         )
  12.     )
  13. )

And another, rather inefficient:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo3 ( l / x )
  2.     (if (setq x (car l))
  3.         (cons
  4.             (vl-remove-if-not   '(lambda ( y ) (= x y)) l)
  5.             (foo3 (vl-remove-if '(lambda ( y ) (= x y)) (cdr l)))
  6.         )
  7.     )
  8. )
« Last Edit: November 25, 2015, 04:20:55 PM by Lee Mac »

udaaf

  • Guest
Re: List Same Data
« Reply #2 on: November 25, 2015, 11:05:25 PM »
Hi Lee,

Thanks you :).
I'll try.