emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Filterring a table for values in a list
@ 2016-06-05 10:11 sven.garbade
  0 siblings, 0 replies; only message in thread
From: sven.garbade @ 2016-06-05 10:11 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

based on a post on
http://emacs.stackexchange.com/questions/20129/how-can-i-filter-table-in-org-mode
about filtering a table row-wise, I want to write a filter to select rows
which matches some values in a list:

#+NAME: table1
| col1  | col2 | col3 | col4 | col5 |
|-------+------+------+------+------|
| row0  |    0 | CH   | CH   |    0 |
| row1  |    2 | D    | CN   |    5 |
| row2  |    4 | USA  | PL   |   10 |
| row3  |    6 | CN   | D    |   15 |
| row4  |    8 | JP   | USA  |   20 |
| row5  |   10 | PL   | PL   |   25 |
| row6  |   12 | USA  | JP   |   30 |
| row7  |   14 | D    | CN   |   35 |
| row8  |   16 | PL   | USA  |   40 |
| row9  |   18 | CN   | D    |   45 |
| row10 |   20 | CH   | CH   |   50 |

I like to filter col4 for "USA" and "CN" 

#+NAME: my-filter
#+BEGIN_SRC elisp :var tbl=table1 val='("" "USA" "CN")  :colnames y
  (cl-loop for row in tbl
           if (member (nth 3 row) val)
           collect row into newtbl
           finally return newtbl)
#+END_SRC

with result

#+RESULTS: my-filter
| row1 |  2 | D  | CN  |  5 |
| row4 |  8 | JP | USA | 20 |
| row7 | 14 | D  | CN  | 35 |
| row8 | 16 | PL | USA | 40 |

My questions:

1) How can I preserve the column names in the result? The code on
http://emacs.stackexchange.com/questions/20129/how-can-i-filter-table-in-org-mode
preserves the table header.
2) Without the "" in the list, only rows matching the last element "CN"
are selected. So I think the code is wrong.

Are there any ideas to improve the code?

Thanks, Sven

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2016-06-05 10:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-05 10:11 Filterring a table for values in a list sven.garbade

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).