Q: Get the values of listed fields in field & write their sum into other field

Hello @all,

In my workout-TW I have equipment for exercises, like resistance-bands. These bands I can use in different ways, which gives more or less difficulty to exercises- similar than weight of dumbbells.
So, I have the red band with several fields with the related weight (f.e.: field “red-u-2+8” with the related weight “15” and “red-u-2+7” with the related weight “17” and some more fields like this).

When I now do an exercise, I choose f.e. two resistance bands: The red one & the blue one.
Then I decide, in which weight-config I will use them and write this decision into the field “AdditionalWeight”. Then this field has the value “red-u-2+8 blue-u-1+8”.
My goal is to get the total weight I had in that exercise and to write that sum into another tiddler. >Means: Getting the related field-values (getting the related “15” of the listed field “red-u-2+8” and the same for the blue one), sum all of them and write that sum into another tiddler.

I found some snippets, but do not know, which of them would be the best and above all, how to combine everything. These snippets are:

{{{ [{!!AdditionalWeight}enlist[red-u-2+8]] :then[get[red-u-2+8]] }}}
{{{ [{!!AdditionalWeight}enlist[red-u-1+8]] :then[get[red-u-1+8]] }}}

This gets the values of the listed fields (like “15”). But I have 40 fields like this… :face_with_spiral_eyes:

The summing I could probably do with something like this {{{ [tag[log]get[run]split[ ]sum[]] }}} or this {{{ [tag[log]get[run]enlist-input:raw[]] }}}, but I don’t know how and how to adapt it to my case.

Please help…

If I understand your tiddler field usage correctly…

You have a “red band” equipment tiddler with fields:
red-u-2+8 = 15
red-u-2+7 = 17
and a “blue band” equipment tiddler with:
blue-u-1+8 = (some value you didn’t specify in your post)
and an exercise tiddler with a field:
AdditionalWeight = red-u-2+8 blue-u-1+8
which is a space-separated list of field names from various equipment tiddlers.

Assuming the exercise tiddler is the currentTiddler, you can get the individual equipment field names from the AdditionalWeight field, using this filter:

{{{ [enlist{!!AdditionalWeight}] }}}

Note: if the exercise tiddler is NOT the currentTiddler, you can use this filter:

{{{ [[SomeExerciseTiddler]get[AdditionalWeight]enlist-input[]] }}}

or perhaps something like this:

Enter exercise tiddler name: <$edit-text field=ex/>
{{{ [{!!ex}get[AdditionalWeight]enlist-input[]] }}}

Next, you want to get the related weight from each of those listed equipment fields. To do this, you can add a :map filter run, like this:

{{{ [enlist{!!AdditionalWeight}] :map[all[tiddlers]get<currentTiddler>] }}}

Note that within the :map filter run, the value of <currentTiddler> is automatically set to each of the listed “AdditionalWeight” field names (i.e., red-u-2+8 and blue-u-1+8). The all[tiddlers] filter operator tells the :map filter run to look for those field names in ANY tiddler.

The next step is to add up those retrieved related weight values, using a +[sum[]] filter run, like this:

{{{ [enlist{!!AdditionalWeight}] :map[all[tiddlers]get<currentTiddler>] +[sum[]] }}}

The final step is to provide a $button that uses $action-setfield to save the total value, like this:

<$let total={{{ [enlist{!!AdditionalWeight}] :map[all[tiddlers]get<currentTiddler>] +[sum[]] }}}>
<$button>save total<$action-setfield $tiddler="AnotherTiddler" totalweight=<<total>>/></$button>
</$let>

-e

@EricShulman Yes, you understood everything like I meant it.
And it is working like I hoped :slight_smile:
So- again a huge thankyou!
Now I am not so far away anymore from my workout-doing :smiling_face_with_sunglasses: :flexed_biceps:
Well, the last thing- the button- I did not yet test, because now I have a lot of work to do -to combine this also with other functions and then implement all this. So the button comes at last. But that is the simplest of all, so I am sure, also that one will work.

@EricShulman I tried to find it out by myself, but successless…
Is it also possible to not only list all related weights with {{{ [enlist{!!AdditionalWeight}] :map[all[tiddlers]get<currentTiddler>] }}} like this 10 7 5,
but additionally to display the listed items like red-u-2+8 & red-u-2+7 plus the related value (weight) next to it, so that it gives something like red-u-2+8 = 15, red-u-2+7 = 17 ?

Try this:

{{{ [enlist{!!AdditionalWeight}]
   :map[all[tiddlers]get<currentTiddler>addprefix[ = ]addprefix<currentTiddler>]
   +[join[, ]] }}}

-e

I did not think, that this would matter so much, but unfortunately it does…
So, normally your code {{{ [enlist{!!AdditionalWeight}] :map[all[tiddlers]get<currentTiddler>addprefix[ = ]addprefix<currentTiddler>] +[join[, ]] }}} would work.
But… I already had a filter to show the red-u-2+8 & red-u-2+7, but with several search&replace including colored formatting to make it better readable.
I thought, no problem, I can combine both.
But… I can’t…

This is my code:

<$list filter="[<currentTiddler>list[!!AdditionalWeight]search-replace:g[GewKl-],[]search-replace[orange-],[@@.orangesband ''Oranges''@@ @@.orangesband Band @@]search-replace[lang-],[>in Schlaufe ]search-replace[rot-],[@@.rotesband ''Rotes''@@ @@.rotesband Band @@]search-replace[u-],[>in den Schlaufen ]search-replace:g:regexp[0],[''0'']search-replace:g:regexp[1],[''1'']search-replace:g:regexp[2],[''2'']search-replace:g:regexp[3],[''3'']search-replace:g:regexp[4],[''4'']search-replace:g:regexp[5],[''5'']search-replace:g:regexp[6],[''6'']search-replace:g:regexp[7],[''7'']search-replace:g:regexp[8],[''8'']search-replace:g:regexp[9],[''9'']search-replace:g:[+],[''+'']]" variable="item" emptyMessage="@@color:red;&emsp;''--''@@"><li style="margin-left:20px;"><<item>> </li></$list>

That gives the result (with some color) f.e.:

  • Rotes Band >in den Schlaufen 2 + 8
  • Rotes Band >in den Schlaufen 2 + 7

I would like to get the result like this:

  • Rotes Band >in den Schlaufen 2 + 8 =15
  • Rotes Band >in den Schlaufen 2 + 7 =17

Sorry, that I thought, I could write it by myself… :flushed_face:

You are trying to do too much in a single filter. Try breaking it out like this:

<$list filter="[enlist{!!AdditionalWeight}]" variable="item" emptyMessage="@@color:red;&emsp;''--''@@">
   <$let out={{{
      [<item>]
     +[search-replace[GewKl-],[]]
     +[search-replace[lang-],[>in Schlaufe ]]
     +[search-replace[u-],[>in den Schlaufen ]]
     +[search-replace[orange-],[@@color:orange; ''Oranges'' Band @@]]
     +[search-replace[rot-],[@@color:red; ''Rotes'' Band @@]]
     +[search-replace:g:regexp[(\d+|\+)],[''$1'']]
   }}}>
   <li style="margin-left:20px;">
   <<out>> = <$text text={{{ [<item>] :map[all[tiddlers]get<currentTiddler>] }}}/>
   </li>
   </$let>
</$list>

Notes:

  • The $list widget just gets each item without doing any formatting
  • The $let then applies a filter to perform replacements that format the item for output
    • The band colors are shown using direct CSS styles instead of classes
    • The numbers and “+” symbol are made bold using a regexp “capture group” (the parens and $1 syntax).
    • Note that \d+ matches any sequence of one or more numeric digits
  • The value for each item is retrieved by using the same :map[...] filter technique as before

-e

Hi @Noushka
Please do not start your questions with HowTo?:

HowTo’s are reserved for posts that describe a solution in a step by step way.

@EricShulman Fantastic. I already tried to build kind of nested filter. But not successful…
Yours is working perfect. I only changed a bit the coloring, because the color-classes didn’t mean only the fontcolor. But that was no problem.
Thanks a ton!

@pmario Oh sorry, didn’t know that. But exactly such reasons I always wrote in front of my topic after the HowTo a questionmark- (if I did not forgot it :roll_eyes:).
So, now I saw that somebody /you replaced my “HowTo?” with a “Q:” Should questions for help be marked that way?

The Q: is not necessarily needed. But since you had a HowTo? in front I used Q: instead, since your titles where not formulated as questions.

If the title has a ? at the end IMO Q: is not needed.