all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to pass table to SRC block as strings only?
@ 2017-01-19  6:55 Sébastien Brisard
  2017-01-19 17:07 ` Charles C. Berry
  0 siblings, 1 reply; 6+ messages in thread
From: Sébastien Brisard @ 2017-01-19  6:55 UTC (permalink / raw)
  To: emacs-orgmode

Hello all,
here is a MWE

=====BEGIN MWE=====

#+NAME: table20170119
| col1 | col2       |
|------+------------|
| row1 | 1234567890 |
| row2 | a          |
| row3 | b          |
| row4 | c          |

#+BEGIN_SRC emacs-lisp :var table=table20170119 :colnames yes :results output
  (print (map 'list (lambda (row) (nth 1 row)) table))
#+END_SRC

#+RESULTS:
:
: (1234567890.0 "a" "b" "c")

=====END MWE=====

As you can see, col #1, row #1 is parsed as a float. I would like it
to be parsed as a string. In other words, the expected RESULTS are

=====BEGIN EXPECTED RESULT=====

#+RESULTS:
:
: ("1234567890" "a" "b" "c")

=====END EXPECTED RESULT=====

Is there an option to prevent org from interpreting the cell values? I
noticed that org-table-to-lisp returns all cells as strings. That's
the functionality I am looking for, in header arguments.

Thanks for your help,
Sébastien

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to pass table to SRC block as strings only?
  2017-01-19  6:55 How to pass table to SRC block as strings only? Sébastien Brisard
@ 2017-01-19 17:07 ` Charles C. Berry
  2017-01-20  2:34   ` Sébastien Brisard
  0 siblings, 1 reply; 6+ messages in thread
From: Charles C. Berry @ 2017-01-19 17:07 UTC (permalink / raw)
  To: Sébastien Brisard; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 959 bytes --]

On Thu, 19 Jan 2017, Sébastien Brisard wrote:

> Hello all,
> here is a MWE
>
> =====BEGIN MWE=====
>
> #+NAME: table20170119
> | col1 | col2       |
> |------+------------|
> | row1 | 1234567890 |
> | row2 | a          |
> | row3 | b          |
> | row4 | c          |
>
> #+BEGIN_SRC emacs-lisp :var table=table20170119 :colnames yes :results output
>  (print (map 'list (lambda (row) (nth 1 row)) table))
> #+END_SRC
>
> #+RESULTS:
> :
> : (1234567890.0 "a" "b" "c")
>
> =====END MWE=====
>
> As you can see, col #1, row #1 is parsed as a float.


Actually, it is not a float:

#+BEGIN_SRC emacs-lisp :var table=table20170119 :colnames yes :results pp
(number-to-string (nth 1 (car table)))
#+END_SRC

#+RESULTS:
: "1234567890"

Maybe this is what you want:

#+BEGIN_SRC emacs-lisp :var table=table20170119 :colnames yes :results pp
(map 'list (lambda (row) (format "%s" (nth 1 row))) table)
#+END_SRC

#+RESULTS:
: ("1234567890" "a" "b" "c")

HTH,

Chuck

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to pass table to SRC block as strings only?
  2017-01-19 17:07 ` Charles C. Berry
@ 2017-01-20  2:34   ` Sébastien Brisard
  2017-01-20  3:49     ` Charles C. Berry
  0 siblings, 1 reply; 6+ messages in thread
From: Sébastien Brisard @ 2017-01-20  2:34 UTC (permalink / raw)
  To: Charles C. Berry, emacs-orgmode

Thanks Charles for this answer. Let me state the problem more clearly.
Number-like cells *are* converted to numbers (as best illustrated by
the example below (see the use of numberp), which might incur accuracy
loss (see below, the first row has a lot of significant digits).
I am not interested in the number representation of these cells, only
the string matters for my application. Due to this accuracy loss,
converting back the number to a string is not an option for me...

Any ideas? Thanks!
Sébastien

===== begin example =====
#+NAME: table20170119
| col1 | col2                 |
|------+----------------------|
| row1 | 12345678901234567890 |
| row2 | a                    |
| row3 | b                    |
| row4 | c                    |

#+BEGIN_SRC emacs-lisp :var table=table20170119 :colnames yes :results output
  (print (map 'list (lambda (row) (nth 1 row)) table))
#+END_SRC

#+RESULTS:
:
: (1.2345678901234567e+019 "a" "b" "c")

#+BEGIN_SRC emacs-lisp :var table=table20170119 :colnames yes :results value
  (map 'list (lambda (row) (numberp (nth 1 row))) table)
#+END_SRC

#+RESULTS:
| t | nil | nil | nil |

===== end example =====

2017-01-19 18:07 GMT+01:00 Charles C. Berry <ccberry@ucsd.edu>:
> On Thu, 19 Jan 2017, Sébastien Brisard wrote:
>
>> Hello all,
>> here is a MWE
>>
>> =====BEGIN MWE=====
>>
>> #+NAME: table20170119
>> | col1 | col2       |
>> |------+------------|
>> | row1 | 1234567890 |
>> | row2 | a          |
>> | row3 | b          |
>> | row4 | c          |
>>
>> #+BEGIN_SRC emacs-lisp :var table=table20170119 :colnames yes :results
>> output
>>  (print (map 'list (lambda (row) (nth 1 row)) table))
>> #+END_SRC
>>
>> #+RESULTS:
>> :
>> : (1234567890.0 "a" "b" "c")
>>
>> =====END MWE=====
>>
>> As you can see, col #1, row #1 is parsed as a float.
>
>
>
> Actually, it is not a float:
>
> #+BEGIN_SRC emacs-lisp :var table=table20170119 :colnames yes :results pp
> (number-to-string (nth 1 (car table)))
> #+END_SRC
>
> #+RESULTS:
> : "1234567890"
>
> Maybe this is what you want:
>
> #+BEGIN_SRC emacs-lisp :var table=table20170119 :colnames yes :results pp
> (map 'list (lambda (row) (format "%s" (nth 1 row))) table)
> #+END_SRC
>
> #+RESULTS:
> : ("1234567890" "a" "b" "c")
>
> HTH,
>
> Chuck

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to pass table to SRC block as strings only?
  2017-01-20  2:34   ` Sébastien Brisard
@ 2017-01-20  3:49     ` Charles C. Berry
  2017-01-21  5:30       ` Sébastien Brisard
  0 siblings, 1 reply; 6+ messages in thread
From: Charles C. Berry @ 2017-01-20  3:49 UTC (permalink / raw)
  To: Sébastien Brisard; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1686 bytes --]

On Thu, 19 Jan 2017, Sébastien Brisard wrote:

> Thanks Charles for this answer. Let me state the problem more clearly.
> Number-like cells *are* converted to numbers (as best illustrated by
> the example below (see the use of numberp), which might incur accuracy
> loss (see below, the first row has a lot of significant digits).
> I am not interested in the number representation of these cells, only
> the string matters for my application. Due to this accuracy loss,
> converting back the number to a string is not an option for me...
>
> Any ideas? Thanks!

The usual resolution of table references will eventually use
`org-babel--string-to-number' to do what its name suggests.

You can write an elisp function to handle references as you wish and call 
them from :var arguments.

A hackish way to do this for your case is to quash the action of 
`org-babel--string-to-number':

#+BEGIN_SRC emacs-lisp
   (defun get-ref-strings-as-is (ref)
     (cl-letf (((symbol-function 'org-babel--string-to-number)
 	       (lambda (x) x)))
       (org-babel-ref-resolve ref)))
#+END_SRC

#+header: :var table=(get-ref-strings-as-is "table20170119") 
#+BEGIN_SRC emacs-lisp :colnames yes :results pp
table
#+END_SRC

#+RESULTS:
: (("row1" "12345678901234567890")
:  ("row2" "a")
:  ("row3" "b")
:  ("row4" "c"))

You can look at `org-babel-ref-resolve' to get some ideas on how to do 
this more artfully.

> Sébastien
>
> ===== begin example =====
> #+NAME: table20170119
> | col1 | col2                 |
> |------+----------------------|
> | row1 | 12345678901234567890 |
> | row2 | a                    |
> | row3 | b                    |
> | row4 | c                    |
>

HTH,

Chuck

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to pass table to SRC block as strings only?
  2017-01-20  3:49     ` Charles C. Berry
@ 2017-01-21  5:30       ` Sébastien Brisard
  2017-01-21  5:46         ` Sébastien Brisard
  0 siblings, 1 reply; 6+ messages in thread
From: Sébastien Brisard @ 2017-01-21  5:30 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode

Hi Chuck,
thank you *very much* for this answer!
I was indeed wondering what was the entry point into the org source
for this. `org-babel-ref-resolve' is where I should start. I'm still
relatively new to emacs-lisp and do not know how to instrument the
code in order to trace all calls (which would probably have solved my
problem).
Anyway, with my version (8.2.10) of org-mode, I had to redefine
`org-babel-read' rather than `org-babel--string-to-number'. Will
update and see where it goes.

In any case, I will consider this problem as solved! Thanks again,
Sébastien

2017-01-20 4:49 GMT+01:00 Charles C. Berry <ccberry@ucsd.edu>:
> On Thu, 19 Jan 2017, Sébastien Brisard wrote:
>
>> Thanks Charles for this answer. Let me state the problem more clearly.
>> Number-like cells *are* converted to numbers (as best illustrated by
>> the example below (see the use of numberp), which might incur accuracy
>> loss (see below, the first row has a lot of significant digits).
>> I am not interested in the number representation of these cells, only
>> the string matters for my application. Due to this accuracy loss,
>> converting back the number to a string is not an option for me...
>>
>> Any ideas? Thanks!
>
>
> The usual resolution of table references will eventually use
> `org-babel--string-to-number' to do what its name suggests.
>
> You can write an elisp function to handle references as you wish and call
> them from :var arguments.
>
> A hackish way to do this for your case is to quash the action of
> `org-babel--string-to-number':
>
> #+BEGIN_SRC emacs-lisp
>   (defun get-ref-strings-as-is (ref)
>     (cl-letf (((symbol-function 'org-babel--string-to-number)
>                (lambda (x) x)))
>       (org-babel-ref-resolve ref)))
> #+END_SRC
>
> #+header: :var table=(get-ref-strings-as-is "table20170119") #+BEGIN_SRC
> emacs-lisp :colnames yes :results pp
> table
> #+END_SRC
>
> #+RESULTS:
> : (("row1" "12345678901234567890")
> :  ("row2" "a")
> :  ("row3" "b")
> :  ("row4" "c"))
>
> You can look at `org-babel-ref-resolve' to get some ideas on how to do this
> more artfully.
>
>> Sébastien
>>
>> ===== begin example =====
>> #+NAME: table20170119
>> | col1 | col2                 |
>> |------+----------------------|
>> | row1 | 12345678901234567890 |
>> | row2 | a                    |
>> | row3 | b                    |
>> | row4 | c                    |
>>
>
> HTH,
>
> Chuck

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to pass table to SRC block as strings only?
  2017-01-21  5:30       ` Sébastien Brisard
@ 2017-01-21  5:46         ` Sébastien Brisard
  0 siblings, 0 replies; 6+ messages in thread
From: Sébastien Brisard @ 2017-01-21  5:46 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode

Hi again,
as a follow up: I can't believe how far behind my org version was (I
suppose I was running the built-in version).
Now that I have upgraded to 9.0.3, your code works like a charm.
Thanks again,
Sébastien

2017-01-21 6:30 GMT+01:00 Sébastien Brisard <sebastien.brisard@m4x.org>:
> Hi Chuck,
> thank you *very much* for this answer!
> I was indeed wondering what was the entry point into the org source
> for this. `org-babel-ref-resolve' is where I should start. I'm still
> relatively new to emacs-lisp and do not know how to instrument the
> code in order to trace all calls (which would probably have solved my
> problem).
> Anyway, with my version (8.2.10) of org-mode, I had to redefine
> `org-babel-read' rather than `org-babel--string-to-number'. Will
> update and see where it goes.
>
> In any case, I will consider this problem as solved! Thanks again,
> Sébastien
>
> 2017-01-20 4:49 GMT+01:00 Charles C. Berry <ccberry@ucsd.edu>:
>> On Thu, 19 Jan 2017, Sébastien Brisard wrote:
>>
>>> Thanks Charles for this answer. Let me state the problem more clearly.
>>> Number-like cells *are* converted to numbers (as best illustrated by
>>> the example below (see the use of numberp), which might incur accuracy
>>> loss (see below, the first row has a lot of significant digits).
>>> I am not interested in the number representation of these cells, only
>>> the string matters for my application. Due to this accuracy loss,
>>> converting back the number to a string is not an option for me...
>>>
>>> Any ideas? Thanks!
>>
>>
>> The usual resolution of table references will eventually use
>> `org-babel--string-to-number' to do what its name suggests.
>>
>> You can write an elisp function to handle references as you wish and call
>> them from :var arguments.
>>
>> A hackish way to do this for your case is to quash the action of
>> `org-babel--string-to-number':
>>
>> #+BEGIN_SRC emacs-lisp
>>   (defun get-ref-strings-as-is (ref)
>>     (cl-letf (((symbol-function 'org-babel--string-to-number)
>>                (lambda (x) x)))
>>       (org-babel-ref-resolve ref)))
>> #+END_SRC
>>
>> #+header: :var table=(get-ref-strings-as-is "table20170119") #+BEGIN_SRC
>> emacs-lisp :colnames yes :results pp
>> table
>> #+END_SRC
>>
>> #+RESULTS:
>> : (("row1" "12345678901234567890")
>> :  ("row2" "a")
>> :  ("row3" "b")
>> :  ("row4" "c"))
>>
>> You can look at `org-babel-ref-resolve' to get some ideas on how to do this
>> more artfully.
>>
>>> Sébastien
>>>
>>> ===== begin example =====
>>> #+NAME: table20170119
>>> | col1 | col2                 |
>>> |------+----------------------|
>>> | row1 | 12345678901234567890 |
>>> | row2 | a                    |
>>> | row3 | b                    |
>>> | row4 | c                    |
>>>
>>
>> HTH,
>>
>> Chuck

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-01-21  5:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-01-19  6:55 How to pass table to SRC block as strings only? Sébastien Brisard
2017-01-19 17:07 ` Charles C. Berry
2017-01-20  2:34   ` Sébastien Brisard
2017-01-20  3:49     ` Charles C. Berry
2017-01-21  5:30       ` Sébastien Brisard
2017-01-21  5:46         ` Sébastien Brisard

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.