How to calculate (sum, avg) with fields and store the result in an other field?

1,030 views
Skip to first unread message

Uwe

unread,
Aug 2, 2017, 4:05:14 PM8/2/17
to TiddlyWiki
Hello dear helpers,

in some tiddlers I have some (< 20) fields with numbers in them, ie {{!!field1}}=2, {{!!field2}}=4 and {{!!field3}}=5 and so on.

I imported $:/plugins/mklauber/math.js from http://mklauber.github.io/tiddly-mathjs/

Using <$calc>, I cannot add the sum of these three fields  {{!!field4}}=<$calc>{{!!field1}} + {{!!field2}} + {{!!field3}}</$calc> into field4.
What went wrong?

I need a solution for automatically calculating with the values of my fields (+, -, *, :, percent, average) and to store the result in an other named field. Is there such a solution out there? (Statistical values calculated about all tiddlers with these fields would be great.)

The use case is to calculate the average points of students they got in some tests (and to get the average of all students).

Greetings,

Uwe





versuch01.html

@TiddlyTweeter

unread,
Aug 2, 2017, 4:21:07 PM8/2/17
to TiddlyWiki
Welcome to the club of non-functioning math in TW.

Its a mess of half solutions.

Josiah

Danielo Rodríguez

unread,
Aug 2, 2017, 4:44:33 PM8/2/17
to TiddlyWiki
Tobías has a good plugin for math

@TiddlyTweeter

unread,
Aug 2, 2017, 5:13:16 PM8/2/17
to TiddlyWiki
Right. Its at: ???? Where?

I hate we can't find anything when we need it.

Mark S.

unread,
Aug 2, 2017, 7:54:31 PM8/2/17
to TiddlyWiki

Danielo Rodríguez

unread,
Aug 3, 2017, 7:20:07 AM8/3/17
to TiddlyWiki
Sorry Josiah, I was on my mobile phone and the link was not easily reachable from here. Luckily someone has already posted the links. Both are very good plugins for math

Thomas Elmiger

unread,
Aug 3, 2017, 8:38:45 AM8/3/17
to TiddlyWiki
I have some links and minimalist comments in my own half solution here, at the end of the readme: https://tid.li/tw5/plugins.html#%24%3A%2Fplugins%2Ftelmiger%2Frpn

No average function available here.

All the best,
Thomas

Matthew Lauber

unread,
Aug 4, 2017, 2:30:53 PM8/4/17
to TiddlyWiki
The issue is that the assignment doesn't work like that.  You can't assign to a field like that, regardless of whether it's math or text or anything else.  The equals sign is just another letter when it's used in html.  You'd probably need to play around with macros and the <$action-setfield> widget to get what you want to happen. AS you can see in your example. The <$calc> widget did add the two fields together resulting in the 8 you see in the example.

Matt Lauber

Matthew Lauber

unread,
Aug 4, 2017, 2:50:33 PM8/4/17
to TiddlyWiki
Here's a wiki with what you want, I think.  You have to click the calculate button to put the value in the field.  


On Wednesday, August 2, 2017 at 4:05:14 PM UTC-4, Uwe wrote:
versuch01.html

Uwe

unread,
Aug 8, 2017, 5:50:03 AM8/8/17
to TiddlyWiki
I totally agree.

Nonetheless, thanks to any contribution to this issue, maybe it helps others.

Here is the solution, I made:


<$fieldmangler>
  <$list filter="[is[current]tag[Aktiv]]">
    <$button message="tm-remove-tag" param="Aktiv">&not;Aktiv</$button>
  </$list>
  <$list filter="[is[current]!tag[Aktiv]]">
    <$button message="tm-add-tag" param="Aktiv">Aktiv</$button>
  </$list>
</$fieldmangler>
<$fieldmangler>
  <$list filter="[is[current]tag[m]]">
    <$button message="tm-remove-tag" param="m">&not;m</$button>
  </$list>
  <$list filter="[is[current]!tag[m]]">
    <$button message="tm-add-tag" param="m">m</$button>
  </$list>
</$fieldmangler>
<$fieldmangler>
  <$list filter="[is[current]tag[w]]">
    <$button message="tm-remove-tag" param="w">&not;w</$button>
  </$list>
  <$list filter="[is[current]!tag[w]]">
    <$button message="tm-add-tag" param="w">w</$button>
  </$list>
</$fieldmangler>
<$fieldmangler>
  <$list filter="[is[current]tag[FW51]]">
    <$button message="tm-remove-tag" param="FW51">&not;FW51</$button>
  </$list>
  <$list filter="[is[current]!tag[FW51]]">
    <$button message="tm-add-tag" param="FW51">FW51</$button>
  </$list>
</$fieldmangler>
<$fieldmangler>
  <$list filter="[is[current]tag[FW61]]">
    <$button message="tm-remove-tag" param="FW61">&not;FW61</$button>
  </$list>
  <$list filter="[is[current]!tag[FW61]]">
    <$button message="tm-add-tag" param="FW61">FW61</$button>
  </$list>
</$fieldmangler>
<$fieldmangler>
  <$list filter="[is[current]tag[FW71]]">
    <$button message="tm-remove-tag" param="FW71">&not;FW71</$button>
  </$list>
  <$list filter="[is[current]!tag[FW71]]">
    <$button message="tm-add-tag" param="FW71">FW71</$button>
  </$list>
</$fieldmangler>
<$fieldmangler>
  <$list filter="[is[current]tag[Facharbeit]]">
    <$button message="tm-remove-tag" param="Facharbeit">&not;Facharbeit</$button>
  </$list>
  <$list filter="[is[current]!tag[Facharbeit]]">
    <$button message="tm-add-tag" param="Facharbeit">Facharbeit</$button>
  </$list>
</$fieldmangler>
<!-- Berechne die Summen für alle tiddler, die mit FW51 getaggt sind. Das kann später der tag ctr sein.-->

{{!!title}}

[[Wirtschaftsinformatik]]

US01 KA01 + KA02: <$edit-text field='fw51-wi-ka01' size=2/> + <$edit-text field='fw51-wi-ka02' size=2/> = {{!!fw51-wi-ka1u2}}
<br>
US02 KA03 + KA04: <$edit-text field='fw51-wi-ka03' size=2/> + <$edit-text field='fw51-wi-ka04' size=2/> = {{!!fw51-wi-ka3u4}}
<br>
//= {{!!fw51-wi-ka1b4}} Punkte Zwischenzeugnis//
<br>

MS01 KA05 + KA06: <$edit-text field='fw51-wi-ka05' size=2/> + <$edit-text field='fw51-wi-ka06' size=2/> = {{!!fw51-wi-ka5u6}}
<br>
= ''{{!!fw51-us-os-wi-summe}}'' Punkte Abschlusszeugnis
<br>

Päd. Punkte Abschlusszeugnis WI: <$edit-text field='fw51-wi-zeugnis_abschluss' size=2/>
<br>

<$button>WI: Aktualisieren und Speichern
<$action-sumfield2 $filter='[tag[FW51]]' $sumfield='fw51-wi-ka01' $sumfield2='fw51-wi-ka02' $storefield='fw51-wi-ka1u2'/>
<$action-sumfield2 $filter='[tag[FW51]]' $sumfield='fw51-wi-ka03' $sumfield2='fw51-wi-ka04' $storefield='fw51-wi-ka3u4'/>
<$action-sumfield2 $filter='[tag[FW51]]' $sumfield='fw51-wi-ka1u2' $sumfield2='fw51-wi-ka3u4' $storefield='fw51-wi-ka1b4'/>
<$action-sumfield2 $filter='[tag[FW51]]' $sumfield='fw51-wi-ka05' $sumfield2='fw51-wi-ka06' $storefield='fw51-wi-ka5u6'/>
<$action-sumfield2 $filter='[tag[FW51]]' $sumfield='fw51-wi-ka1u2' $sumfield2='fw51-wi-ka3u4' $storefield='fw51-us-wi-summe'/>
<$action-sumfield2 $filter='[tag[FW51]]' $sumfield='fw51-us-wi-summe' $sumfield2='fw51-wi-ka5u6' $storefield='fw51-us-os-wi-summe'/>
</$button>

---

{{!!title}}

[[Controlling]]

MS02 KA01 + KA02: <$edit-text field='fw51-ctr-ka01' size=2/> + <$edit-text field='fw51-ctr-ka02' size=2/>
<br>//= {{!!fw51-ctr-ka1u2}} Punkte Zwischenzeugnis// <br>

OS01 KA03 + KA04: <$edit-text field='fw51-ctr-ka03' size=2/> + <$edit-text field='fw51-ctr-ka04' size=2/> = {{!!fw51-ctr-ka3u4}}
<br>
= ''{{!!fw51-ctr-summe}}'' Punkte Abschlusszeugnis
<br>

Päd. Punkte Abschlusszeugnis CTR: <$edit-text field='fw51-ctr-zeugnis_abschluss' size=2/>
<br>

<$button>CTR: Aktualisieren und Speichern
<$action-sumfield2 $filter='[tag[FW51]]'| $sumfield='fw51-ctr-ka01' $sumfield2='fw51-ctr-ka02'| $storefield='fw51-ctr-ka1u2'/>
<$action-sumfield2 $filter='[tag[FW51]]'| $sumfield='fw51-ctr-ka03' $sumfield2='fw51-ctr-ka04'| $storefield='fw51-ctr-ka3u4'/>
<$action-sumfield2 $filter='[tag[FW51]]'| $sumfield='fw51-ctr-ka1u2' $sumfield2='fw51-ctr-ka3u4'| $storefield='fw51-ctr-summe'/>
</$button>

And this is the (button based) solution only for adding the percentage based points, not for the points themselves, not for the grades.

Automatically calculating values in the fields and in the tiddler like this example would be a great simplification:

{{!!schnitt}}=({{!!field1}}+{{!!field2}}+{{!!field3}}+{{!!field4}})/4


To comfortably calc with (more than two) fields is often needed. Perhaps some day a developer will see this problem and solve it. Let me know. ;-)

Greetings,

Uwe

Mat

unread,
Aug 8, 2017, 7:09:54 AM8/8/17
to TiddlyWiki
Uwe wrote:
 
Automatically calculating values in the fields and in the tiddler like this example would be a great simplification:

{{!!schnitt}}=({{!!field1}}+{{!!field2}}+{{!!field3}}+{{!!field4}})/4
 

While it's not direct (i.e not optimized), it should not be overly difficult to make a macro with a listwidget and the argument "( field1 + field2 + field3 + field4 ) / 4" and iterates through this to pairwise (i.e two numbers and one operator at a time) apply one of the existing calculators (e.g Thomas Elmigers rpn plugin). 

But then, there must be a zillion great js calculators that could just be wrapped up as a TW plugin (...he said confidently without having ever made a js plugin)

<:-)

Uwe

unread,
Aug 8, 2017, 10:28:44 AM8/8/17
to TiddlyWiki
So I have tried this with Thomas Elmigers rpn plugin, found here:
https://tid.li/tw5/plugins.html
https://tid.li/tw5/hacks.html#rpnTest


<$macrocall $name="rpn" a={{!!field1}} b={{!!field2}} operation="+" 2/>

<$macrocall $name="rpn" a={{!!field3}} b={{!!field4}} operation="+" 2/>

It is also not possible to add more than 2 fields in a macrocall.

But how can I add the results of each of these expressions? (Has it something to do with the listwidget you mentioned?)

Perhaps anyone has an idea for a completely other solution of the problem (see first post). [Please not: An external link to an Excel spreadsheet ;-)]

Mark S.

unread,
Aug 8, 2017, 10:43:46 AM8/8/17
to TiddlyWiki
A different approach would be to use mathcell (http://mathcell.tiddlyspot.com/) that emulates spreadsheet-like abilities.

Good luck,
Mark

Mat

unread,
Aug 8, 2017, 1:01:16 PM8/8/17
to TiddlyWiki
Uwe wrote:
So I have tried this with Thomas Elmigers rpn plugin, 
But how can I add the results of each of these expressions? (Has it something to do with the listwidget you mentioned?)

Yes,  listwidget. I have not tried it so I may miss something fundamental but, basically, you'd iterate through the arguments recursively using the rpn to calculate "up til now" along the way. I'm a bit unsure of how to handle parentheses but spontaneously I'd think the first listwidget filtering must be to split out such sub-statements (i.e surrounded with parentheses) and then do the iteration with the rpn on it's contents. If you could accept statements without parentheses, I think it would be more direct and simpler. Regardless; if you have limited experience with filter operators in TW... then I guess you have some fun ahead of you ;-) ...BUT, as noted, I don't think it would make for an effective solution given that there surely are optimized js calculators that can(?) be 'tiddlified'.

<:-)

Thomas Elmiger

unread,
Aug 10, 2017, 2:36:12 AM8/10/17
to TiddlyWiki
Hi Uwe

Try this structure to sum 4 fields in 1 macrocall:

<$macrocall $name="rpn" a=<<rpn "{{rpnTest!!example-1}}" "{{rpnTest!!example-1}}" +>> b=<<rpn "{{rpnTest!!example-1}}" "{{rpnTest!!example-1}}" +>> operation="+"/>

Hint: in the shortcut <<syntax>> the tiddler from where to get the field value must be specified in the transclusion.

All the best,
Thomas


Am Dienstag, 8. August 2017 16:28:44 UTC+2 schrieb Uwe:
So I have tried this with Thomas Elmigers rpn plugin, found here:
https://tid.li/tw5/plugins.html
https://tid.li/tw5/hacks.html#rpnTest


<$macrocall $name="rpn" a={{!!field1}} b={{!!field2}} operation="+" 2/>

<$macrocall $name="rpn" a={{!!field3}} b={{!!field4}} operation="+" 2/>

It is also not possible to add more than 2 fields in a macrocall.
 
Well, as demonstrated, it IS possible, but not very intuitive ;–)
And: I would not recommend to try to add another level – the code gets too complicated. So 4 operands would be the limit.
Reply all
Reply to author
Forward
0 new messages