emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: <sven.garbade@evall.org>
To: emacs-orgmode@gnu.org
Subject: Filterring a table for values in a list
Date: Sun, 05 Jun 2016 12:11:14 +0200	[thread overview]
Message-ID: <87zir07xod.fsf@uranus.home> (raw)

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

                 reply	other threads:[~2016-06-05 10:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zir07xod.fsf@uranus.home \
    --to=sven.garbade@evall.org \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).