Selectively summing together field values?

53 views
Skip to first unread message

Dave Kor

unread,
Dec 15, 2021, 9:40:06 PM12/15/21
to TiddlyWiki
Hello,  I am storing numeric data in tiddler fields and I'd like to select fields that start with lets say "aaa" and sum all their values together. I know I can use filters to select the fields i need using something like {{{ [[Data]fields[]prefix[aaa]] }}} and I can probably sum their values together using the sum[] operator. 

However, I am stumped as to how I can get the field's value. I know the final filter would look something like {{{ =[[Data]fields[]prefix[aaa]???] +[sum[]] }}} I just do not know what operator to use to get the field values. 

Charlie Veniot

unread,
Dec 15, 2021, 10:41:18 PM12/15/21
to TiddlyWiki
Hello Dave,

That is no trivial thing you're trying to do.  Well, maybe somebody has a trivial solution.

But what a great "Brain Age" game !!!

So in the meantime, download the attached and drag into tiddlywiki.com to import the tiddlers and give you an opportunity to study what I've done.

I've pasted the code below as reference for anything murky that needs some serious explaining.

It is not a trivial affair to require dynamic identification of fields that need to be summed.

Much easier to specify the exact fields.  Then you have a simple filter like "[get[aaa1]] [get[aaa2]] ... etc. ... [get[aaaX]] +[sum[]]"

Since we do not know the exact field names, then we have to dynamically build that filter above.  (As per the highlighted magic in the code below)


Ignoring the starting and ending "pre" tags.  I'm trying that to see if the code below shows up okay in TiddlyTalk (which is finicky with this kind of stuff.)

<pre>

\define p1() [get[
\define p2() ]]
\define p3() +[sum[]]

<$list variable="thisField" filter="[fields[]regexp[aaa]]">
sum of <<thisField>> values:
<$list filter="[get<thisField>sum[]]">
{{!!title}}
</$list><br>
</$list>
<$vars thisDynamicFilter={{{ [fields[]regexp[aaa]addprefix<p1>addsuffix<p2>] [<p3>] +[join[ ]] }}}>
<$list filter={{{ [<thisDynamicFilter>] }}}>
''total:'' <<currentTiddler>>
</$list>
</$vars>


</pre>
SuperDuperSumming.json

Charlie Veniot

unread,
Dec 15, 2021, 10:43:16 PM12/15/21
to TiddlyWiki
ARG!  I forgot to mention, that "regexp" bit might need some tweaking so that the pattern gets all, but only, the fields you are interested in.

Dave Kor

unread,
Dec 16, 2021, 8:04:57 AM12/16/21
to TiddlyWiki
Thanks for the info. After looking through my stuff I realized I had already solved this several months ago. The solution is rather simple:

{{{ [[Data]fields[]prefix[aaa]] :map[[Data]get<currentTiddler>] +[sum[]] }}}

Charlie Veniot

unread,
Dec 16, 2021, 10:18:29 AM12/16/21
to TiddlyWiki

All of your "aaa" fields to be summed are just in the one tiddler.  That changes everything.

I thought your "aaa" fields were spread across multiple tiddlers.  Yeah, my solution is for a much more complex problem.
Reply all
Reply to author
Forward
0 new messages