emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ken Mankoff <mankoff@gmail.com>
To: Org-mode <emacs-orgmode@gnu.org>
Subject: [O] babel bash :var tables - change from associative to index arrays
Date: Sat, 12 Sep 2020 12:21:51 -0700	[thread overview]
Message-ID: <87d02qls1c.fsf@gmail.com> (raw)

Hi Org List,

I'd like to submit a patch so that all bash babel arrays are indexed. Are there cons to this that I'm not seeing? It might break some existing code that expects associative arrays. Could this be a breaking change that is included in Org 5.0? I'd like to check on the likely acceptance of such a patch before I take the time to code it.

Currently Org Babel uses associative arrays for multi-column bash :var inputs, and indexed arrays if single column. Put differently, for 1D tables the array is indexed [0,1,...,n] and for 2D arrays it is indexed using the first column. Example at the bottom of this email.

There are some drawbacks to this method: 1) It drops rows if the first column is not unique (I have tables where the first three columns make a unique index from "|year|month|day|value") and 2) associative arrays do not maintain order, so the code cannot rely on the order of the rows (see example below where "foo2,bar2" order is swapped).

If all arrays are indexed, these two issues would go away.

  -k.


#+NAME: tbl1D
| foo1  |
| bar1  |

#+NAME: tbl2D
| foo2  | 42 |
| bar2  | 24 |

#+NAME: print_tbl
#+BEGIN_SRC bash :var tbl=tbl1D
for key in "${!tbl[@]}"; do
  row=${tbl[$key]}
  echo $key $row
done
#+END_SRC

#+CALL: print_tbl(tbl=tbl1D)

#+RESULTS:
| 0 | foo1 |
| 1 | bar1 |

#+CALL: print_tbl(tbl=tbl2D)

#+RESULTS:
| bar2 | 24 |
| foo2 | 42 |


                 reply	other threads:[~2020-09-12 19:22 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=87d02qls1c.fsf@gmail.com \
    --to=mankoff@gmail.com \
    --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).