Thursday, May 9, 2019

emacs lisp count unique lines in region, sort results by count, elisp


(let (items)
  (dolist (item (split-string (buffer-substring (region-beginning) (region-end)) "\n"))
    (let ((match (assoc item items)))
      (if match
          (setcdr match (1+ (cdr match)))
        (push (cons item 1) items))))

  (pp (sort items (lambda (x y) (< (cdr x) (cdr y))))))

No comments:

Post a Comment