[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: uniq
From: |
Stephen Berman |
Subject: |
Re: uniq |
Date: |
Sat, 04 Dec 2010 13:08:28 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) |
On Fri, 3 Dec 2010 18:41:12 -0800 Tak Ota <address@hidden> wrote:
> Do we have something equivalent to the next command? I know the name
> is bad as it is not same as UNIQ(1). It works better because sorting
> is not required.
>
> -Tak
>
> ;;
> ;; uniq
> ;;
> (defun uniq ()
> "Omit duplicated lines."
> (interactive)
> (save-excursion
> (goto-char (point-min))
> (while (not (= (point) (point-max)))
> (let* ((start (point))
> (str (format "^%s"
> (regexp-quote
> (buffer-substring start
> (progn (forward-line 1)
> (point)))))))
> (save-excursion
> (while (re-search-forward str nil t)
> (delete-region (match-beginning 0) (match-end 0))))))))
Would it be faster to avoid nested while-loops?
(defun uniq ()
"Omit duplicated lines."
(interactive)
(let (lines)
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(let* ((beg (line-beginning-position))
(end (line-end-position))
(line (buffer-substring beg end)))
(if (member line lines)
(delete-region beg (1+ end))
(push line lines)
(forward-line)))))))
Steve Berman
- uniq, Tak Ota, 2010/12/03
- Re: uniq,
Stephen Berman <=