1

I would like to pass only some columns of a table to a script:

#+name: table
#+RESULTS: 
| Col0  | Col1 | Col2 | Col3 |
|-------+------+------+------|
| Line1 |    1 |    2 |    3 |
| Line2 |    4 |    5 |    6 |

#+name: log
#+begin_src emacs-lisp :var data="" :results value
data
#+end_src

#+call: log(data=table[,0;3])

Expected:

#+RESULTS:
| Col0  | Col3 |
|-------+------|
| Line1 |    3 |
| Line2 |    6 |

Actual:

#+RESULTS:
| Line1 | Line2 |

The ; syntax is not in any manual, I made that up :-). How can I get a subset of the columns of a table?

Workaround

It is possible to use Lisp to filter the list ourselves:

#+name: filter-columns
#+begin_src emacs-lisp :var data='() :columns='()
  (let ((result (mapcar (lambda (line)
                          (mapcar (lambda (column) (seq-elt line column)) columns))
                        data)))
    `(,(car result)
      \hline
      ,@(cdr result)))
#+end_src

#+call: log[:colnames no](data=filter-columns[:colnames no](data=table,columns='(0 3))) 

#+RESULTS:
| Col0  | Col3 |
| Line1 |    3 |
| Line2 |    6 |
1

Note: For everyday coding, I use code blocks to filter table data instead of Indexable Variable Values (IVV) because it is usually faster to write and explain the code block than it is to teach others or remember IVV syntax.

Use : to select range of org-table columns when using Indexable Variable Values

"Ranges of variable values can be referenced using two integers separated by a :, in which case the entire inclusive range is referenced."

#+BEGIN_SRC elisp :var data=table[2:3,1:3]  :rownames no :hlines no  :colnames no
  data
#+END_SRC

#+RESULTS:
| 1 | 2 | 3 |
| 4 | 5 | 6 |

Use code blocks to combine non contiguous columns and more advanced filtering

#+BEGIN_SRC elisp :var c0=table[,0] :var c3=table[,3]  :rownames no :hlines no  :colnames no
  (cl-mapcar 'list c0 c3)
#+END_SRC

#+RESULTS:
| Col0  | Col3 |
| Line1 |    3 |
| Line2 |    6 |

Thank you for asking your question!


This answer was validated using:

emacs version: GNU Emacs 27.1
org-mode version: 9.3.7

2
  • I marked your answer as accepted but what you propose is not much better than the workaround I wrote after the question. Thank you for answering anyway and confirming there is nothing in the IVV syntax that will help me. – Damien Cassou Sep 8 '20 at 15:59
  • 1
    @DamienCassou - Thank you for choosing my answer. I completely agree that workaround is the only feasible option using the built-in code. I liked the ; idea but unfortunately my elisp skills are not high enough to extend the built-in IVV syntax to make your idea a reality. – Melioratus Sep 8 '20 at 16:09

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.