Add values of two fields and store the result as another field

Every tiddler tagged ‘class’ has two fields ‘boys’ and ‘girls’ holding the number of boys and girls respectively in each class. The title holds the class name X-A, X-B, etc. The body (text) carries some description of the class.

I want to add another field ‘tot_students’ that will be sum of ‘boys’ and ‘girls’ for each ‘class’ tiddler.

How do I do that?

Hello!

You’ll need to decide between two approaches:

(1) display a dynamic total wherever you want to see it (which doesn’t require a field at all)
<$text text={{{ [{!!boys}add{!!girls}] }}}/>

(2) bake that actual numeric total into a field, using the actionsetfield widget.

Of course, you can put the first expression into a tot_students field. Still, that field will not behave as a number for the purposes of further calculations, until/unless you put an actual number in that field.

Extending on Springer’s answer: if you just need to see the student total —their (1) suggestion—, you can get tiddlywiki to show you that calculation in all tiddlers with the “class” tag by extending the tiddler template as follows:

Create a tiddler with any name, add to it the tag “$:/tags/ViewTemplate” (without quotes), with the following content:

<$list filter="[<currentTiddler>tag[class]]">

Girls: {{!!girls}}<br>
Boys: {{!!boys}}<br>
Total Students: <$text text={{{ [{!!boys}add{!!girls}] }}}/>

</$list>
1 Like

@Springer @jerojasro Thanks. I want to add a separate field to the tiddler. So, merely displaying the total would not work.

I looked up ActionSetField widget. And could come up with this.

<$button>
  <$list filter="[<currentTiddler>tag[class]]">
    <$set name="boys" tiddler=<currentTiddler> field="boys">
      <$set name="girls" tiddler=<currentTiddler> field="girls">
        Need the code to sum two variables and assign that sum to tot_students variable
        <$action-setfield $tiddler=<currentTiddler> tot_students="value of tot_students variable"/>
      </$set>
    </$set>
  </$list>
</$button>

But all my attempts to fill the gaps proved futile. My problem would be solved if you could help me fill the gaps.

try this:

<$list filter="[<currentTiddler>tag[class]]">
<$button>
        <$action-setfield $tiddler=<<currentTiddler>> tot_students={{{ [{!!boys}add{!!girls}] }}}/>
SET IT
</$button>
</$list>

You were nearly done; I’ve made some fixes:

  • using variables outside of TW filters has to be done like this: <<boys>>; while inside a filter ([]), you use a single bracket: [<boys>add<girls>]
  • I added the code from @springer to do the arithmetic: {{{ [{!!boys}add{!!girls}] }}}, and adjusted it to use the variables you defined: {{{ [<boys>add<girls>] }}}

Just in case, that triple-curly-brace syntax is called a Filtered Transclusion, and it’s what one uses to put the result of a filter operation (arithmetic op in this case) in the body of your tiddler, or as part of html/widget tags, like the tot_students=... widget attribute here.

2 Likes