emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Shankar Rao <shankar.rao@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Re: How to cross reference more than two table columns?
Date: Sun, 11 Oct 2015 12:53:08 -0700	[thread overview]
Message-ID: <CAGEgU=hnkNqDTpQEkSy7uM19OqOnN7sCzpe7xYwRRs3HGXJqcA@mail.gmail.com> (raw)

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

Sorry forgot to put in a subject

On Sun, Oct 11, 2015 at 12:51 PM, Shankar Rao <shankar.rao@gmail.com> wrote:

> I have multiple savings accounts, some of which I want to partition into
> mutiple virtual "subaccounts" that don't merit their own savings account. I
> want to use org-mode tables to keep track how much money is each
> subaccount. Given the following table:
>
> #+TBLNAME: trans-150925
> | ! | Amount | From       | To         | Subacct     |
> |---+--------+------------+------------+-------------|
> |   |     50 | Short Term | Inbox      | Bond        |
> |   |    200 | Inbox      | Short Term | Bond        |
> |   |    120 | Gifts      | Inbox      | Baby Gifts  |
> |   |     75 | Short Term | Inbox      | Furnishings |
>
> In this example, I have accounts "Inbox", "Short Term", and "Gifts". Of
> these accounts, "Short Term" has two subaccounts "Bond" and "Furnishings",
> and "Gifts" has a subaccount "Baby Gifts". I want to create a function that
> given a subaccount name, calculates the net amount to be
> withdrawn/deposited to the subaccount. Note that "Inbox" doesn't have any
> subaccounts, which I want to use to determine if a given transaction is a
> withdrawal or deposit to the subaccount.
>
> I have created the following two functions to implement this:
>
> (defun sbr/org-lookup-trans (category date s-name r-name)
>   (let* ((table (concat "trans-" date))
>          (s-range (format "@I%s..@>%s" s-name s-name))
>          (s-col (org-table-get-remote table s-range))
>          (r-range (format "@I%s..@>%s" r-name r-name))
>          (r-col (org-table-get-remote table r-range)))
>     (org-lookup-all category s-col r-col)))
>
> (defun sbr/get-net-subacct (category date)
>   (let* ((from-vec (sbr/org-lookup-trans category date "$Subacct" "$From"))
>   (to-vec (sbr/org-lookup-trans category date "$Subacct" "$To"))
>          (amount-vec (sbr/org-lookup-trans category date "$Subacct"
> "$Amount"))
>          (from-inbox-ind <indices of all from-vec elements that equal
> "Inbox">) ; A
>        (to-inbox-ind  <indices of all to-vec elements that equal
> "Inbox">) ; B
>        (adds amount-vec[from-inbox-ind]) ; C
>        (subs amount-vec[to-inbox-ind]))  ; D
>     (calc-eval (if (zerop (length subs))
>                  adds
>  (concat adds "-" subs)))))
>
> I need help filling in the pseudo code in lines A through D, namely:
>
> 1. how to get a list of all indices of elements in a vector that satisfy a
> predicate?
> 2. how to reference a vector by a list of indices?
>
> In this example the function should return the following values:
>
> (sbr/get-net-subacct "Bond" "150925") ==> 150
> (sbr/get-net-subacct "Furnishings" "150925") ==> -75
> (sbr/get-net-subacct "Baby Gifts" "150925") ==> -120
>
> How do I implement the above pseudocode functions? Is there a simpler way
> to accomplish this task?
>

[-- Attachment #2: Type: text/html, Size: 6124 bytes --]

                 reply	other threads:[~2015-10-11 19:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAGEgU=hnkNqDTpQEkSy7uM19OqOnN7sCzpe7xYwRRs3HGXJqcA@mail.gmail.com' \
    --to=shankar.rao@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).