Summing an attribute of all inbound links

(Will Farr) #1


I’m a new Tinderbox user, and am really looking forward to making the most of it (and the forum!). Right now I am having some difficulty achieving the following; I can’t seem to figure it out from the reference website/manuals:

  • I have a “Target” note with 3 inbound links.
  • I would like to sum $MyNumber of the inbound linking notes “Target” and store that in $MyNumber(“Target”).
  • I am trying to use a rule of “Target” to retrieve and sum each linking notes’ $MyNumber. I can’t seem to find the operator to sum the set of numbers that is returned from (links.inbound…$MyNumber) and store it in a Number attribute. I found that I am able to store the returned set in a string attribute, however ($MyString=links.inbound…$MyNumber)

I’m not having any luck using sum(links.inbound…$MyNumber), $MyNumber), either. I feel like I’m missing an obvious syntax error, but can’t spot it.

TLDR: How do I make a note that sums the values of a particular attribute of the notes that link to it?

Thanks! Looking forward to learning and contributing.


(Mark Anderson) #2

Firstly, notes already store (as a read-only calculated value, their inbound link count in $InboundLinkCount. Thus a rule in note ‘Target’ could do this:

$MyNumber = $InboundLinkCount;

If you wanted to do this for ‘Target’ but running code in a different note, sue the following (this assumes ‘Target’ is a unique note name in the document):

$MyNumber("Target") = $InboundLinkCount("Target");

Howeverm I thinkwhat you are actually after are sum() or sum_if() and possibly links().

(Galen Menzel) #3

I do this regularly enough to have prototypes that take care of this in my standard Tinderbox starter file.

Here’s a working example. (67.1 KB) The “Sum”, “Product”, and “Average” notes will calculate over all incoming links of type “value” using the value of $MyNumber from the linking note. More exotic operations can be added simply enough by modifying the existing prototypes. Note that this is all done with Edicts rather than Rules so you will have to hit ⌃⌘= to force a full recalculation from time to time.

A screenshot for the lazy and/or difficult to excite:

In the case of “Sum”, the relevant action code is

links.inbound.value.$MyNumber.each(x) { $Sum = $Sum + x; };

(For the sake of clarity, this differs slightly from the action code that appears in the document.)

You can find more info on the links operator at aTbRef.

(Will Farr) #4

Thank you very much; this is exactly what I needed!