<$list> output as a single string?

104 views
Skip to first unread message

hubertgk

unread,
Jun 18, 2018, 11:54:10 AM6/18/18
to TiddlyWiki
Hi,

Using Evan Balster's <<formula>> macro, I'm trying to sum the numerical output of a <$list> filter but each of the list items is processed separately.

For example, if the output of my list is 2 2 1 and I try to sum that with 5 using <<formula>>, then each of the arguments is summed in isolation and I get "7 7 6" (2+5, 2+5, 1+5) instead of "10" (2+2+1+5).

So my question is, how can I transform the output of a list filter (all returned arguments) into a single array containing those arguments?

Here's my code. I've provided more of it for context but it's not necessarily relevant to my question (I'm having a list of values returned based on a search performed on indexes in a data tiddler. The values are numbers and the output is a list).

\define test()
<$list filter="[[_data]getindex[$(matching_indexes)$]]" variable="dataTiddler">
<$set name=arguments value=<<dataTiddler>>>
<<test2>>
</$set>
</
$list>
\end

\define test2()
<<formula "sum(($(arguments)$), 5)">>
\end

<$set name=matching_indexes value=searchterm>

<<test>>

</$set>


Thank you very much for your ideas!

Best,

Mark S.

unread,
Jun 18, 2018, 12:15:13 PM6/18/18
to TiddlyWiki
The SetWidget now has a filter option. I seem to recall that the output is as an array. Perhaps you could replace the <$list> with a <$set> ?

For CarboCommander (http://carbocommander.tiddlyspot.com/) I added a couple macro's to Evan's kit to allow the aggregation of numbers into variables. It may be klunky (you have to use 3 different macros to initialize, accumulate, and report "variables" ) The variables in this case refer to identifying strings rather than your usual TW variables.

Good luck!
-- Mark

TonyM

unread,
Jun 18, 2018, 8:48:27 PM6/18/18
to TiddlyWiki

hubertgk,

When you say
"
sum that with 5" I presumed you mean add 5 to the single number found for each item in your output list, because you are calling Test2 every items.

I think if I understand correctly You need to divide the problem into two steps. One sum the value of each list item together then two add 5 to the total. To do this you will need a variable outside of the list to accrues the values of the items inside the list.

I have not done this yet, but I need to do this for another piece of work so I will provide a solution sooner or later.

Counting or adding values from lists or field in sets of tiddlers should be a basic and accessible function in TiddlyWiki and it would be good if it did not need additional plugins.

Regards
Tony 


TonyM

unread,
Jun 18, 2018, 9:03:51 PM6/18/18
to TiddlyWiki
Post Script,

I just started to investigate using Evans Formula plugin and it permits the provision of filters into the sum, so you do not need a list 

eg;
(= sum([tag[Expenses]get[value]]) =)

so perhaps this would work to return the number

(= sum([[_data]getindex[$(matching_indexes)$]])+5 =)

Notice the +5 is outside the sum

Regards
Tony

TonyM

unread,
Jun 19, 2018, 12:31:46 AM6/19/18
to TiddlyWiki

\define count-items(filter) <$list filter="$filter$ +[count[]]" variable=result><<result>></$list>
\define sum-field(fieldname,filter)
<$set name=filter value="$filter$" emptyValue="[has:field[$fieldname$]]">
(= sum([has[$fieldname$]] +[get[$fieldname$]]) =)
</$set>
\end

You provide the count items with a filter and it will count the members in that set

<<count-items "filter">>

You need Evans Formula Plugin for 
You provide the sum-Field with a fieldname and it will sum the contents of that field all tiddlers with that fieldname
<<sum-field "fieldname">>

Or provide a filter to limit the tiddlers
<<sum-field "fieldname" "filtername">>




hubertgk

Since I went down this line for myself I created the following two macros you can place in a tiddler tagged $:/tags/Macro


Regards
Tony

TonyM

unread,
Jun 19, 2018, 12:35:06 AM6/19/18
to TiddlyWiki
Mark,

I am keen to see these macros you refer to can you tell me the tiddler names in http://carbocommander.tiddlyspot.com/ ?

Also, I would love to find a way to do sums without needing to bring in the formulas plugin as it is somewhat overkill.

I may put a request in for a sum[] filter operator that resembles the count[] filter operator.

Thank in advance

Mark S.

unread,
Jun 19, 2018, 11:15:29 AM6/19/18
to TiddlyWiki
The macros are in

$:/plugins/mas/formula/functions/aggregation

You're right -- a "sum" filter seems like a useful thing and wouldn't be too hard to write. I suppose someone has already done it ...

-- Mark

Stobot

unread,
Jun 19, 2018, 11:22:12 AM6/19/18
to TiddlyWiki
I agree - a sum[] filter operator would be hugely valuable! I like the plugin but it's often overkill for simple needs. sum[] should be core in my opinion.

hubertgk

unread,
Jun 20, 2018, 7:35:40 AM6/20/18
to TiddlyWiki
Thank you all for the suggestions! Much appreciated.

Best,
Hubert
Reply all
Reply to author
Forward
0 new messages