* Two tables with same data but different sorting
@ 2020-10-01 8:55 Axel Kielhorn
2020-10-01 12:21 ` John Kitchin
0 siblings, 1 reply; 11+ messages in thread
From: Axel Kielhorn @ 2020-10-01 8:55 UTC (permalink / raw)
To: Org-Mode Mailing List
Hello!
I have a table that I want to show with two different sorting orders but I don’t want to maintain the data twice.
* Table 1
| Manufacturer | Name | Price |
|-----------------+-------------+-------|
| ACME | super cheep | 25 $ |
| Roadrunner Inc. | Kaboom | 27 $ |
| ACME | cheep | 30 $ |
* Table 2
| Manufacturer | Name | Price |
|-----------------+-------------+-------|
| ACME | cheep | 30 $ |
| ACME | super cheep | 25 $ |
| Roadrunner Inc. | Kaboom | 27 $ |
Is there a way to do this in org?
Right now I copy the table and apply an =C-c ^ a= on the first column.
Greetings
Axel
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Two tables with same data but different sorting
2020-10-01 8:55 Two tables with same data but different sorting Axel Kielhorn
@ 2020-10-01 12:21 ` John Kitchin
2020-10-01 14:36 ` Axel Kielhorn
0 siblings, 1 reply; 11+ messages in thread
From: John Kitchin @ 2020-10-01 12:21 UTC (permalink / raw)
To: Axel Kielhorn; +Cc: Org-Mode Mailing List
[-- Attachment #1: Type: text/plain, Size: 1797 bytes --]
You could do something like this:
* Table 1
#+name: table1
| Manufacturer | Name | Price |
|-----------------+-------------+-------|
| ACME | super cheep | 25 $ |
| Roadrunner Inc. | Kaboom | 27 $ |
| ACME | cheep | 30 $ |
#+RESULTS: resorted
| Manufacturer | Name | Price |
|-----------------+-------------+-------|
| ACME | super cheep | 25 $ |
| ACME | cheep | 30 $ |
| Roadrunner Inc. | Kaboom | 27 $ |
** Code for resorting
#+name: resorted
#+BEGIN_SRC emacs-lisp :var data=table1 :colnames t
(sort data (lambda (row1 row2) (string< (first row1) (first row2))))
#+END_SRC
John
-----------------------------------
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu
On Thu, Oct 1, 2020 at 4:57 AM Axel Kielhorn <org-mode@axelkielhorn.de>
wrote:
> Hello!
>
> I have a table that I want to show with two different sorting orders but I
> don’t want to maintain the data twice.
>
>
> * Table 1
>
> | Manufacturer | Name | Price |
> |-----------------+-------------+-------|
> | ACME | super cheep | 25 $ |
> | Roadrunner Inc. | Kaboom | 27 $ |
> | ACME | cheep | 30 $ |
>
> * Table 2
>
> | Manufacturer | Name | Price |
> |-----------------+-------------+-------|
> | ACME | cheep | 30 $ |
> | ACME | super cheep | 25 $ |
> | Roadrunner Inc. | Kaboom | 27 $ |
>
> Is there a way to do this in org?
> Right now I copy the table and apply an =C-c ^ a= on the first column.
>
> Greetings
> Axel
>
[-- Attachment #2: Type: text/html, Size: 2567 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Two tables with same data but different sorting
2020-10-01 12:21 ` John Kitchin
@ 2020-10-01 14:36 ` Axel Kielhorn
2020-10-01 15:47 ` John Kitchin
0 siblings, 1 reply; 11+ messages in thread
From: Axel Kielhorn @ 2020-10-01 14:36 UTC (permalink / raw)
To: Org-Mode Mailing List
> Am 01.10.2020 um 14:21 schrieb John Kitchin <jkitchin@andrew.cmu.edu>:
>
> You could do something like this:
>
>
> * Table 1
>
> #+name: table1
> | Manufacturer | Name | Price |
> |-----------------+-------------+-------|
> | ACME | super cheep | 25 $ |
> | Roadrunner Inc. | Kaboom | 27 $ |
> | ACME | cheep | 30 $ |
>
> #+RESULTS: resorted
> | Manufacturer | Name | Price |
> |-----------------+-------------+-------|
> | ACME | super cheep | 25 $ |
> | ACME | cheep | 30 $ |
> | Roadrunner Inc. | Kaboom | 27 $ |
>
> ** Code for resorting
>
> #+name: resorted
> #+BEGIN_SRC emacs-lisp :var data=table1 :colnames t
> (sort data (lambda (row1 row2) (string< (first row1) (first row2))))
> #+END_SRC
>
> John
>
Thanks John, this is really powerful.
I changed =first row1= to =elt row1 6= since my real table is more complex.
(Again I learned a little bit more about elisp.)
Greetings
Axel
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Two tables with same data but different sorting
2020-10-01 14:36 ` Axel Kielhorn
@ 2020-10-01 15:47 ` John Kitchin
2020-10-02 6:25 ` Axel Kielhorn
0 siblings, 1 reply; 11+ messages in thread
From: John Kitchin @ 2020-10-01 15:47 UTC (permalink / raw)
To: Axel Kielhorn; +Cc: Org-Mode Mailing List
[-- Attachment #1: Type: text/plain, Size: 1511 bytes --]
Glad it was helpful. You might also try (seventh row1) or (nth 6 row1). I
think it is the same thing, but more obvious to read!
John
-----------------------------------
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu
On Thu, Oct 1, 2020 at 10:37 AM Axel Kielhorn <org-mode@axelkielhorn.de>
wrote:
>
>
> > Am 01.10.2020 um 14:21 schrieb John Kitchin <jkitchin@andrew.cmu.edu>:
> >
> > You could do something like this:
> >
> >
> > * Table 1
> >
> > #+name: table1
> > | Manufacturer | Name | Price |
> > |-----------------+-------------+-------|
> > | ACME | super cheep | 25 $ |
> > | Roadrunner Inc. | Kaboom | 27 $ |
> > | ACME | cheep | 30 $ |
> >
> > #+RESULTS: resorted
> > | Manufacturer | Name | Price |
> > |-----------------+-------------+-------|
> > | ACME | super cheep | 25 $ |
> > | ACME | cheep | 30 $ |
> > | Roadrunner Inc. | Kaboom | 27 $ |
> >
> > ** Code for resorting
> >
> > #+name: resorted
> > #+BEGIN_SRC emacs-lisp :var data=table1 :colnames t
> > (sort data (lambda (row1 row2) (string< (first row1) (first row2))))
> > #+END_SRC
> >
> > John
> >
>
> Thanks John, this is really powerful.
>
> I changed =first row1= to =elt row1 6= since my real table is more complex.
>
> (Again I learned a little bit more about elisp.)
>
> Greetings
> Axel
>
>
>
[-- Attachment #2: Type: text/html, Size: 2446 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Two tables with same data but different sorting
2020-10-01 15:47 ` John Kitchin
@ 2020-10-02 6:25 ` Axel Kielhorn
2020-10-02 9:36 ` Robert Pluim
2020-10-02 21:04 ` John Kitchin
0 siblings, 2 replies; 11+ messages in thread
From: Axel Kielhorn @ 2020-10-02 6:25 UTC (permalink / raw)
To: Org-Mode Mailing List
> Am 01.10.2020 um 17:47 schrieb John Kitchin <jkitchin@andrew.cmu.edu>:
>
> Glad it was helpful. You might also try (seventh row1) or (nth 6 row1). I think it is the same thing, but more obvious to read!
I agree that „first second …“ would be easier for an english speaker.
Having the ordinal number 1 based but the nth number 0 based is irritating (and sadly there is no „last“ or „penultimate“).
Actually I was looking for something like last element or the element before the last element.
(nth -1 row1) for the last row would be fine, but I guess that is the Python whispering in my ear.
Combined with the right :exports I now get what I want.
Thanks again for this additional information.
Greetings Axel
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Two tables with same data but different sorting
2020-10-02 6:25 ` Axel Kielhorn
@ 2020-10-02 9:36 ` Robert Pluim
2020-10-05 8:08 ` Axel Kielhorn
2020-10-02 21:04 ` John Kitchin
1 sibling, 1 reply; 11+ messages in thread
From: Robert Pluim @ 2020-10-02 9:36 UTC (permalink / raw)
To: Axel Kielhorn; +Cc: Org-Mode Mailing List
>>>>> On Fri, 2 Oct 2020 08:25:03 +0200, Axel Kielhorn <org-mode@axelkielhorn.de> said:
>> Am 01.10.2020 um 17:47 schrieb John Kitchin <jkitchin@andrew.cmu.edu>:
>>
>> Glad it was helpful. You might also try (seventh row1) or (nth 6 row1). I think it is the same thing, but more obvious to read!
Axel> I agree that „first second …“ would be easier for an english speaker.
Axel> Having the ordinal number 1 based but the nth number 0 based is irritating (and sadly there is no „last“ or „penultimate“).
? C-h f last
last is a compiled Lisp function in `subr.el'.
(last LIST &optional N)
Probably introduced at or before Emacs version 1.1.
This function does not change global state, including the match data.
Return the last link of LIST. Its car is the last element.
If LIST is nil, return nil.
If N is non-nil, return the Nth-to-last link of LIST.
If N is bigger than the length of LIST, return LIST.
Axel> Actually I was looking for something like last element or the element before the last element.
element before last would be
(car (last lst 2))
Robert
--
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Two tables with same data but different sorting
2020-10-02 9:36 ` Robert Pluim
@ 2020-10-05 8:08 ` Axel Kielhorn
2020-10-05 8:32 ` Robert Pluim
0 siblings, 1 reply; 11+ messages in thread
From: Axel Kielhorn @ 2020-10-05 8:08 UTC (permalink / raw)
To: Org-Mode Mailing List
> Am 02.10.2020 um 11:36 schrieb Robert Pluim <rpluim@gmail.com>:
>
>>>>>> On Fri, 2 Oct 2020 08:25:03 +0200, Axel Kielhorn <org-mode@axelkielhorn.de> said:
>
>>> Am 01.10.2020 um 17:47 schrieb John Kitchin <jkitchin@andrew.cmu.edu>:
>>>
>>> Glad it was helpful. You might also try (seventh row1) or (nth 6 row1). I think it is the same thing, but more obvious to read!
>
> Axel> I agree that „first second …“ would be easier for an english speaker.
> Axel> Having the ordinal number 1 based but the nth number 0 based is irritating (and sadly there is no „last“ or „penultimate“).
>
> ? C-h f last
>
> last is a compiled Lisp function in `subr.el'.
>
> (last LIST &optional N)
>
> Probably introduced at or before Emacs version 1.1.
> This function does not change global state, including the match data.
>
> Return the last link of LIST. Its car is the last element.
> If LIST is nil, return nil.
> If N is non-nil, return the Nth-to-last link of LIST.
> If N is bigger than the length of LIST, return LIST.
As i understand it, this should work:
* Table
#+name: table1
| Manufacturer | Name | Price |
|-----------------+-------------+-------|
| ACME | super cheep | 127 $ |
| Roadrunner Inc. | Kaboom | 27 $ |
| ACME | cheep | 30 $ |
#+RESULTS: resorted
| Manufacturer | Name | Price |
|-----------------+-------------+-------|
| ACME | super cheep | 127 $ |
| ACME | cheep | 30 $ |
| Roadrunner Inc. | Kaboom | 27 $ |
** Code for resorting
#+name: resorted
#+BEGIN_SRC emacs-lisp :var data=table1 :colnames t
(sort data (lambda (row1 row2) (string< (last row1) (last row2))))
#+END_SRC
But I get:
Wrong type argument: stringp, ("30 $“)
Greeting Axel
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Two tables with same data but different sorting
2020-10-05 8:08 ` Axel Kielhorn
@ 2020-10-05 8:32 ` Robert Pluim
2020-10-05 9:21 ` Axel Kielhorn
0 siblings, 1 reply; 11+ messages in thread
From: Robert Pluim @ 2020-10-05 8:32 UTC (permalink / raw)
To: Axel Kielhorn; +Cc: Org-Mode Mailing List
>>>>> On Mon, 5 Oct 2020 10:08:08 +0200, Axel Kielhorn <org-mode@axelkielhorn.de> said:
From the docstring:
>> Return the last link of LIST. Its car is the last element.
Axel> But I get:
Axel> Wrong type argument: stringp, ("30 $“)
You need to do (car (last ...))
and you'll want some calls to 'string-to-number', since
(string< "30 $" "127 $")
=> nil
Robert
--
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Two tables with same data but different sorting
2020-10-05 8:32 ` Robert Pluim
@ 2020-10-05 9:21 ` Axel Kielhorn
2020-10-05 9:45 ` Robert Pluim
0 siblings, 1 reply; 11+ messages in thread
From: Axel Kielhorn @ 2020-10-05 9:21 UTC (permalink / raw)
To: Org-Mode Mailing List
> Am 05.10.2020 um 10:32 schrieb Robert Pluim <rpluim@gmail.com>:
>
>>>>>> On Mon, 5 Oct 2020 10:08:08 +0200, Axel Kielhorn <org-mode@axelkielhorn.de> said:
>
> From the docstring:
>>> Return the last link of LIST. Its car is the last element.
>
> Axel> But I get:
> Axel> Wrong type argument: stringp, ("30 $“)
>
> You need to do (car (last …))
So =first= returns an element, but =last= returns a list.
I get it.
> If N is non-nil, return the Nth-to-last link of LIST.
I was interpreting link as element and didn’t look at the docstring.
> and you'll want some calls to 'string-to-number', since
>
> (string< "30 $" "127 $")
> => nil
Yes, I know. But I wanted to keep the change as minimal as possible.
Thanks for you help, my example is working now.
Greetings Axel
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Two tables with same data but different sorting
2020-10-05 9:21 ` Axel Kielhorn
@ 2020-10-05 9:45 ` Robert Pluim
0 siblings, 0 replies; 11+ messages in thread
From: Robert Pluim @ 2020-10-05 9:45 UTC (permalink / raw)
To: Axel Kielhorn; +Cc: Org-Mode Mailing List
>>>>> On Mon, 5 Oct 2020 11:21:24 +0200, Axel Kielhorn <org-mode@axelkielhorn.de> said:
>> Am 05.10.2020 um 10:32 schrieb Robert Pluim <rpluim@gmail.com>:
>>
>>>>>>> On Mon, 5 Oct 2020 10:08:08 +0200, Axel Kielhorn <org-mode@axelkielhorn.de> said:
>>
>> From the docstring:
>>>> Return the last link of LIST. Its car is the last element.
>>
Axel> But I get:
Axel> Wrong type argument: stringp, ("30 $“)
>>
>> You need to do (car (last …))
Axel> So =first= returns an element, but =last= returns a list.
Axel> I get it.
You'll have to get a time machine and take it up with the common lisp
folks from about 40 years ago :-)
Robert
--
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Two tables with same data but different sorting
2020-10-02 6:25 ` Axel Kielhorn
2020-10-02 9:36 ` Robert Pluim
@ 2020-10-02 21:04 ` John Kitchin
1 sibling, 0 replies; 11+ messages in thread
From: John Kitchin @ 2020-10-02 21:04 UTC (permalink / raw)
To: Axel Kielhorn; +Cc: emacs-orgmode
I don't know of something built in, but dash provides a few things sort
of like that:
#+BEGIN_SRC emacs-lisp :results raw
(-last-item '(a b c))
#+END_SRC
#+RESULTS:
c
#+BEGIN_SRC emacs-lisp :results raw
(-slice '(a b c) -1)
#+END_SRC
#+RESULTS:
(c)
#+BEGIN_SRC emacs-lisp :results raw
(-take-last 1 '(a b c))
#+END_SRC
#+RESULTS:
(c)
surprisingly, it does not seem to support negative indices, but I guess
it would not be hard to make a wrapper that does that.
Axel Kielhorn <org-mode@axelkielhorn.de> writes:
>> Am 01.10.2020 um 17:47 schrieb John Kitchin <jkitchin@andrew.cmu.edu>:
>>
>> Glad it was helpful. You might also try (seventh row1) or (nth 6 row1). I think it is the same thing, but more obvious to read!
>
> I agree that „first second …“ would be easier for an english speaker.
> Having the ordinal number 1 based but the nth number 0 based is irritating (and sadly there is no „last“ or „penultimate“).
>
> Actually I was looking for something like last element or the element before the last element.
>
> (nth -1 row1) for the last row would be fine, but I guess that is the Python whispering in my ear.
>
> Combined with the right :exports I now get what I want.
>
> Thanks again for this additional information.
>
> Greetings Axel
--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2020-10-05 9:46 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-01 8:55 Two tables with same data but different sorting Axel Kielhorn
2020-10-01 12:21 ` John Kitchin
2020-10-01 14:36 ` Axel Kielhorn
2020-10-01 15:47 ` John Kitchin
2020-10-02 6:25 ` Axel Kielhorn
2020-10-02 9:36 ` Robert Pluim
2020-10-05 8:08 ` Axel Kielhorn
2020-10-05 8:32 ` Robert Pluim
2020-10-05 9:21 ` Axel Kielhorn
2020-10-05 9:45 ` Robert Pluim
2020-10-02 21:04 ` John Kitchin
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).