[tw5] One approach: summing untold number of fields from untold number of tiddlers

Dynamic creation of filters !!!

Say that one has an untold number of numeric fields spread throughout a TiddlyWiki instance, all of those fields having names that match a pattern.

(For example: say all of the fields have “aaa” in their names.)

And say any one tiddler can have an untold number of those fields (0 to many).

How would we go about summing all of these fields, specifying only a pattern for field names, and without knowing which tiddlers have those fields?

As an exercise, here’s my approach. Download attached and drag into TiddlyWiki.com for study.

For convenience, here is the code of interest:

\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:'' <>

Good fun !

SuperDuperSumming.json (790 Bytes)

Charlie,

For more good fun, I wondered if the dynamic filter part of your solution could be implemented using the new filter features of 5.2.0. Here’s what I came up with that seems to work:

[fields[]regexp[aaa]] :map[all[tiddlers]getsum[]] +[sum[]]

or to match your output:

‘‘total:’’ <$text text={{{ [fields[]regexp[aaa]] :map[all[tiddlers]getsum[]] +[sum[]] }}}/>

Brian

1 Like

It is strange, what I see here in GG is different from what is fetched in Talk!
I think codes need to be wrapped in codeblock!

@admins please have a look!

[fields[]regexp[aaa]] :map[all[tiddlers]getsum[]] +[sum[]]

or to match your output:

‘‘total:’’ <$text text={{{ [fields[]regexp[aaa]] :map[all[tiddlers]getsum[]] +[sum[]] }}}/>

Brian,

This is a bit tricky! I did myself before I saw your solution and I got wrong total sum! The reason was
:map returns one result per input (e.g. the input it receives from [fields[]regexp[aaa]], its upstream filter run)
Your trick here is you have used sum twice! and I think here it makes sense, as you are calculating sum of sum or total sum!
Nice solution! Worth to be documented!

Best wishes
Mohammad

In my own solution I actually used

''total:'' <$text text={{{ [fields[]regexp[aaa]] :map[all[tiddlers]get<currentTiddler>] +[sum[]] }}}/>

and I got 1077!!! The source of error was two tiddlers have field aaa2 and my filter accounted for one! because the way :map works!!