From mboxrd@z Thu Jan 1 00:00:00 1970 From: Pascal Fleury Subject: Export arrays for 'sh' code blocks when using bash Date: Thu, 27 Mar 2014 11:26:55 +0100 Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary=bcaec547c62302cad004f59408f2 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:41155) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WT7Wx-0003a1-MY for emacs-orgmode@gnu.org; Thu, 27 Mar 2014 06:27:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WT7Ww-0002fD-AF for emacs-orgmode@gnu.org; Thu, 27 Mar 2014 06:27:27 -0400 Received: from mail-ve0-x22d.google.com ([2607:f8b0:400c:c01::22d]:39842) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WT7Ww-0002f8-3Z for emacs-orgmode@gnu.org; Thu, 27 Mar 2014 06:27:26 -0400 Received: by mail-ve0-f173.google.com with SMTP id oy12so3774307veb.18 for ; Thu, 27 Mar 2014 03:27:25 -0700 (PDT) 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-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --bcaec547c62302cad004f59408f2 Content-Type: multipart/alternative; boundary=bcaec547c62302cacd04f59408f0 --bcaec547c62302cacd04f59408f0 Content-Type: text/plain; charset=UTF-8 Hello, I'dl like to propose a patch for inclusion into org-mode (ob-shell.el in particular). *TL;DR:* use arrays and associative arrays when exporting variables to bash in 'sh' code blocks. *Details:* When variables are defined in a 'sh' code block, they are exported as strings. when the variable itself is an array or a table, then we simply get a shell variable that contains the list of all values in a non-structured form. E.g. #+NAME: my_list | one | | two | | three | #+NAME: experiment | name | first_attempt | | date | [2014-03-27 Thu] | | user | fleury | #+BEGIN_SRC sh :var scalar="some value" :var array=my_list :var table=config echo ${scalar} # -> prints 'some value' echo ${array} # -> prints 'one two three' echo ${table} # -> prints 'first attempt [2014-03-27 Thu] fleury' #+END_SRC This will print simple strings. Also, there is no easy way to access the date of the experiment, for example. Now bash has things like arrays and associative arrays, but the current ob-shell.el does not use these. Probably because their syntax is bash-specific, and ob-shell.el is shell-agnostic. My patch (attached) changes this in the case you have (setq org-babel-sh-command "bash") in your emacs config somewhere. If any other value is used, it continues to export them as we do today (I don't know enough about other shells). In that case, it will export the list as an array, the table as an associative array, and the scalar as it does already. So the 'sh' code block could then use #+BEGIN_SRC sh :var scalar="some value" :var array=my_list :var table=config echo ${scalar} echo ${array[1]} # -> prints "two" echo ${table[user]} # -> prints "fleury" #+END_SRC In the case we have a bigger table, then the first row is used as key, the others are represented as a string with all the values (as it is for array currently). bash does not have multi-dimensional arrays, so it needs to be a string. This makes writing shell snippets much easier in my experience, and there I'd like to propose this fix for the org-mode community at large. --paf --bcaec547c62302cacd04f59408f0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hello,

I'dl like to propose a patch= for inclusion into org-mode (ob-shell.el in particular).

TL;DR: use arrays and associative arrays when exporting vari= ables to bash in 'sh' code blocks.

Details:
When variables are defined in= a 'sh' code block, they are exported as strings. when the variable= itself is an array or a table, then we simply get a shell variable that co= ntains the list of all values in a non-structured form. E.g.

= #+NAME: my_list
| one =C2=A0 |
| two =C2=A0 |
| three |

#+NAME: experiment
| name | first_attempt= =C2=A0 =C2=A0|
| date | [2014-= 03-27 Thu] |
| user | fleury =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |

#+BEGIN_= SRC sh :var scalar=3D"some value" :var array=3Dmy_list :var table= =3Dconfig
echo ${scalar} =C2=A0# -> prints 'some value'
echo ${array} = =C2=A0 # -> prints 'one two three'
echo ${table} =C2=A0 # -> print= s 'first attempt [2014-03-27 Thu] fleury'
#+END_SRC

This will print simple strings. Also, there is no= easy way to access the date of the experiment, for example. Now bash has t= hings like arrays and associative arrays, but the current ob-shell.el does = not use these. Probably because their syntax is bash-specific, and ob-shell= .el is shell-agnostic.

My patch (attached) changes this in the case you have= =C2=A0
(setq org-babel-sh-c= ommand "bash")
in your emacs config somewher= e. If any other value is used, it continues to export them as we do today (= I don't know enough about other shells).=C2=A0

In that case, it will export the list as an array, the = table as an associative array, and the scalar as it does already. So the &#= 39;sh' code block could then use

#+BEGIN_SRC sh :var scalar=3D"some value= " :var array=3Dmy_list :var table=3Dconfig
echo ${scalar}
= echo ${array[1]} # -> prints "= ;two"
echo ${ta= ble[user]} # -> prints "fleury"
#+END_SRC

In the case we have a bigger table, then the first row is u= sed as key, the others are represented as a string with all the values (as = it is for array currently). bash does not have multi-dimensional arrays, so= it needs to be a string.
=C2=A0
This makes writing shell snippets much easier in = my experience, and there I'd like to propose this fix for the org-mode = community at large.

--paf
--bcaec547c62302cacd04f59408f0-- --bcaec547c62302cad004f59408f2 Content-Type: text/x-patch; charset=US-ASCII; name="orgmode-bash.patch" Content-Disposition: attachment; filename="orgmode-bash.patch" Content-Transfer-Encoding: base64 X-Attachment-Id: f_ht9vtupa0 ZGlmZiAtLWdpdCBhL2xpc3Avb2Itc2hlbGwuZWwgYi9saXNwL29iLXNoZWxsLmVsCmluZGV4IDNl ZGU3MDEuLjBhNjkxZTIgMTAwNjQ0Ci0tLSBhL2xpc3Avb2Itc2hlbGwuZWwKKysrIGIvbGlzcC9v Yi1zaGVsbC5lbApAQCAtMTA2LDYgKzEwNiwzMCBAQCBUaGlzIGZ1bmN0aW9uIGlzIGNhbGxlZCBi eSBgb3JnLWJhYmVsLWV4ZWN1dGUtc3JjLWJsb2NrJy4iCiAKIDs7IGhlbHBlciBmdW5jdGlvbnMK IAorKGRlZnVuIG9yZy1iYWJlbC12YXJpYWJsZS1hc3NpZ25tZW50czpiYXNoX2FycmF5ICh2YXJu YW1lIHZhbHVlcyAmb3B0aW9uYWwgc2VwIGhsaW5lKQorICAiUmV0dXJuc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KGRlZnVuIG9yZy1iYWJl bC1zaC12YXItdG8tc2ggKHZhciAmb3B0aW9uYWwgc2VwIGhsaW5lKQo= --bcaec547c62302cad004f59408f2--