Re: Two tables with same data but different sorting
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 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 >
Re: Two tables with same data but different sorting
> Am 01.10.2020 um 14:21 schrieb John Kitchin : > > 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
Re: Two tables with same data but different sorting
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 wrote: > > > > Am 01.10.2020 um 14:21 schrieb John Kitchin : > > > > 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 > > >
Re: Two tables with same data but different sorting
> Am 01.10.2020 um 17:47 schrieb John Kitchin : > > 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
Re: Two tables with same data but different sorting
> On Fri, 2 Oct 2020 08:25:03 +0200, Axel Kielhorn > said: >> Am 01.10.2020 um 17:47 schrieb John Kitchin : >> >> 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 --
Re: Two tables with same data but different sorting
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 writes: >> Am 01.10.2020 um 17:47 schrieb John Kitchin : >> >> 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
Re: Two tables with same data but different sorting
> Am 02.10.2020 um 11:36 schrieb Robert Pluim : > >> On Fri, 2 Oct 2020 08:25:03 +0200, Axel Kielhorn >> said: > >>> Am 01.10.2020 um 17:47 schrieb John Kitchin : >>> >>> 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
Re: Two tables with same data but different sorting
> On Mon, 5 Oct 2020 10:08:08 +0200, Axel Kielhorn > 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 --
Re: Two tables with same data but different sorting
> Am 05.10.2020 um 10:32 schrieb Robert Pluim : > >> On Mon, 5 Oct 2020 10:08:08 +0200, Axel Kielhorn >> 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
Re: Two tables with same data but different sorting
> On Mon, 5 Oct 2020 11:21:24 +0200, Axel Kielhorn > said: >> Am 05.10.2020 um 10:32 schrieb Robert Pluim : >> >>> On Mon, 5 Oct 2020 10:08:08 +0200, Axel Kielhorn 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 --