From mboxrd@z Thu Jan 1 00:00:00 1970 From: Uwe Brauer Subject: [SOLVED] (was: join two or more tables) Date: Sun, 18 Aug 2019 10:46:43 +0200 Message-ID: <87mug6q2rw.fsf_-_@mat.ucm.es> References: <871rxirifk.fsf@mat.ucm.es> <87tvaeq3hz.fsf@mat.ucm.es> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:50690) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hzGpx-0003L3-M1 for emacs-orgmode@gnu.org; Sun, 18 Aug 2019 04:46:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hzGpw-0007Oa-FX for emacs-orgmode@gnu.org; Sun, 18 Aug 2019 04:46:53 -0400 Received: from 195-159-176-226.customer.powertech.no ([195.159.176.226]:46116 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hzGpw-0007O2-8Y for emacs-orgmode@gnu.org; Sun, 18 Aug 2019 04:46:52 -0400 Received: from list by blaine.gmane.org with local (Exim 4.89) (envelope-from ) id 1hzGps-000kS7-GT for emacs-orgmode@gnu.org; Sun, 18 Aug 2019 10:46:48 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org >>> "UB" == Uwe Brauer writes: >> Hi >> I was googling this a bit, I found more complicated scenarios, but not >> how to join one or more tables. >> For example >> #+begin_src >> #+tblname: nut >> | type | Fiber | Sugar | Protein | Carb | >> |----------+-------+-------+---------+------| >> | eggplant | 2.5 | 3.2 | 0.8 | 8.6 | >> | tomatoe | 0.6 | 2.1 | 0.8 | 3.4 | >> | onion | 1.3 | 4.4 | 1.3 | 9.0 | >> | egg | 0 | 18.3 | 31.9 | 18.3 | >> #+tblname: nut2 >> | type | Fiber | Sugar | Protein | Carb | >> |----------+-------+-------+---------+------| >> | rice | 0.2 | 0 | 1.5 | 16.0 | >> | bread | 0.7 | 0.7 | 3.3 | 16.0 | >> | orange | 3.1 | 11.9 | 1.3 | 17.6 | >> | banana | 2.1 | 9.9 | 0.9 | 18.5 | >> | tofu | 0.7 | 0.5 | 6.6 | 1.4 | >> | nut | 2.6 | 1.3 | 4.9 | 7.2 | >> | corn | 4.7 | 1.8 | 2.8 | 21.3 | >> #+end_src >> I want to generate a new table total-nuts, which is just a combination >> of the two tables, nuts and nuts2 >> Uwe Brauer Best solution found in https://emacs.stackexchange.com/questions/48508/merge-org-tables ** Solution 1 #+NAME: T1 | col1 | col2 | | 1 | 2 | #+NAME: T2 | col3 | | 3 | #+BEGIN_SRC emacs-lisp :var t1=T1 t2=T2 (cl-mapcar #'append t1 t2) #+END_SRC #+RESULTS: | col1 | col2 | col3 | | 1 | 2 | 3 | ** Solution 2 #+NAME: T3 | col1 | col2 | | 11 | 12 | | 21 | 22 | | 31 | 32 | #+NAME: T4 | col3 | | 13 | | 12 | #+BEGIN_SRC emacs-lisp :var t1=T3 t2=T4 :colnames no (cl-mapcar #'append t1 t2) #+END_SRC #+RESULTS: | col1 | col2 | col3 | | 11 | 12 | 13 | | 21 | 22 | 12 | ** Solution 3 #+NAME: TA | col1 | col2 | |------+------| | 1 | 2 | #+NAME: TB | col3 | |------| | 3 | #+BEGIN_SRC emacs-lisp :var t1=TA t2=TB :colnames no (insert-at 1 (cl-mapcar #'append t1 t2) 'hline) #+END_SRC #+RESULTS: | col1 | col2 | col3 | |------+------+------| | 1 | 2 | 3 | #+begin_src elisp :noexport (defun insert-at (n list element) "Insert ELEMENT into LIST at position N. I.e., (eq ELEMENT (nth N (insert-at N LIST ELEMENT)))." (if (eq n 0) (cons element list) (let ((link (nthcdr (1- n) list))) (setcdr link (cons element (cdr link)))) list)) #+end_src