emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Nick Dokos <nicholas.dokos@hp.com>
To: Bastien <bzg@gnu.org>
Cc: emacs-orgmode@gnu.org, Petro <x.piter@gmail.com>
Subject: Re: Table filter.
Date: Tue, 08 May 2012 11:10:37 -0400	[thread overview]
Message-ID: <12562.1336489837@alphaville> (raw)
In-Reply-To: Message from Bastien <bzg@gnu.org> of "Tue, 08 May 2012 14:36:42 +0200." <87zk9izw6d.fsf@gnu.org>

Bastien <bzg@gnu.org> wrote:

> Hi Petro,
> 
> Petro  <x.piter@gmail.com> writes:
> 
> > lets say I have a table
> >
> > | n | 2 | 3 | 4 | 5 | 6 |
> > |---+---+---+---+---+---|
> > | 1 | a |   |   |   |   |
> > | 2 | b |   |   |   |   |
> > | 3 | b |   |   |   |   |
> > | 4 | a |   |   |   |   |
> > | 5 | c |   |   |   |   |
> > | 6 | b |   |   |   |   |
> > | 7 | a |   |   |   |   |
> >
> > I want to filter this table and get only rows where second column equal a
> > | n | 2 | 3 | 4 | 5 | 6 |
> > |---+---+---+---+---+---|
> > | 1 | a |   |   |   |   |
> > | 4 | a |   |   |   |   |
> > | 7 | a |   |   |   |   |
> 
> Such feature does not exist for now, but I would welcome enhancements in
> this direction.  Thanks for this idea,
> 

You can get most of the way there with babel, as long as you don't insist
on modifying the table in place. Something like this e.g. using python 2.x:

--8<---------------cut here---------------start------------->8---
#+BEGIN_SRC elisp
(setq org-babel-min-lines-for-block-output 0)
#+END_SRC

#+RESULTS:
#+begin_example
0
#+end_example

The above setting is just to force an example block. The default value
(10) produces colon-demarcated results.


#+name: orig
| n | 2 | 3 | 4 | 5 | 6 |
|---+---+---+---+---+---|
| 1 | a |   |   |   |   |
| 2 | b |   |   |   |   |
| 3 | b |   |   |   |   |
| 4 | a |   |   |   |   |
| 5 | c |   |   |   |   |
| 6 | b |   |   |   |   |
| 7 | a |   |   |   |   |

#+BEGIN_SRC python :var table=orig :results output
  # print the header
  print "#+name: filtered"
  for x in table:
      if x[1] == 'a':
          print "|%s" % ("|".join(map(str, x)))
#+END_SRC

#+RESULTS:
#+begin_example
#+name: filtered
|1|a||||
|4|a||||
|7|a||||
#+end_example
--8<---------------cut here---------------end--------------->8---

I don't know how to get the column heading row though: apparently babel
strips it from the table it passes to the code block.

Nick

  reply	other threads:[~2012-05-08 15:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-08  9:45 Table filter x.piter
2012-05-08  9:53 ` Bastien
2012-05-08 10:47   ` Petro
2012-05-08 12:36     ` Bastien
2012-05-08 15:10       ` Nick Dokos [this message]
2012-05-08 16:47 ` Marc-Oliver Ihm
2012-05-09  9:29   ` Detlef Steuer

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=12562.1336489837@alphaville \
    --to=nicholas.dokos@hp.com \
    --cc=bzg@gnu.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=x.piter@gmail.com \
    /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).