[tw5] Selectively summing together field values?

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.

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.)

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

<$list variable="thisField" filter="[fields[]regexp[aaa]]">
sum of <> values:
<$list filter="[getsum[]]">
{{!!title}}

**<$vars thisDynamicFilter={{{ [fields[]regexp[aaa]addprefixaddsuffix] [] +[join[ ]] }}}>** <$list filter={{{ **[]** }}}> ''total:'' <>

SuperDuperSumming.json (790 Bytes)

1 Like

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.

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] +[sum[]] }}}

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.