emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [O] babel bash :var tables - change from associative to index arrays
@ 2020-09-12 19:21 Ken Mankoff
  0 siblings, 0 replies; only message in thread
From: Ken Mankoff @ 2020-09-12 19:21 UTC (permalink / raw)
  To: Org-mode

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.


#+NAME: tbl1D
| foo1  |
| bar1  |

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

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

#+CALL: print_tbl(tbl=tbl1D)

| 0 | foo1 |
| 1 | bar1 |

#+CALL: print_tbl(tbl=tbl2D)

| bar2 | 24 |
| foo2 | 42 |

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-09-12 19:22 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-12 19:21 [O] babel bash :var tables - change from associative to index arrays Ken Mankoff

Code repositories for project(s) associated with this public inbox


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).