1

I have one table (TBL1), I want to generate another table with unique items from TBL1. Currently, I can do it with elisp as below but I think it's too complicate.

#+NAME: TBL1
| Item | Amount |
|------+--------|
| A    |    100 |
| B    |     20 |
| A    |    120 |
| C    |     40 |
| B    |     50 |
| A    |     20 |
| C    |     16 |
|------+--------|
#+BEGIN_SRC elisp :var x=TBL1[1:-1] :results output raw :exports both
  (let (y)
    (dolist (line x)
      (setq y (append y (list (car line)))))
    (setq y (delete-dups y))
    (dolist (elem y)
      (princ (format "|%s|\n" elem))))
#+END_SRC

#+RESULTS:
| A |
| B |
| C |

I can find a similar unique API from the document.what's the best way to do that?

1

This should do:

#+BEGIN_SRC elisp :var x=TBL1[1:-1] 
  (delete-dups (mapcar (lambda (y) (list (car y))) x))
#+END_SRC

Its similar to your code, but with a functional programming (rather than imperative) approach.

(list (car y)) returns rows with only the first element of the original rows. mapcar applies this function to the whole list of rows.

Note that the result is the list, which org-mode automatically interprets and converts to a table. No need to explicitly format it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.