Summarizing data tiddlers

116 views
Skip to first unread message

Stobot

unread,
Jan 2, 2020, 9:34:12 AM1/2/20
to TiddlyWiki
Hello All,

I must be missing easy, but is there a way to easily reference the values of data tiddlers within filter notation? I'm trying to build a 'voting' mechanism for my multi-user wiki. My initial thought would be do have each "item to be voted on" as a data tiddler, and store the "score" under their username, so that I can tell how many people voted, and what the average score was. Each data tiddler would be tagged with the name of the vote.

So, I have for instance a data tiddler named "Idea 1", "Idea 2", "Idea 3" and so on. "Idea 1" would look like:
User A:1
User B:2
User C:2
User D:3
User E:1
etc.

Each data tiddler would be tagged "Vote on this" or something. Here's my code so far for the "Vote on this" tiddler:

<table>
<th>Idea</th><th>Vote Count</th><th>Avg Score</th>
<$list filter="[tag<currentTiddler>]">
<tr>
<td>{{!!title}}</td>
<td><$list filter="[all[current]indexes[]count[]]"></$list></td>
<td><$list filter="[all[current]indexes[]get[value]] +sum[]]"></$list></td>
</tr>
</$list>
</table>

The line with the get[value] +sum[] is where I'm stuck. Obviously sum doesn't get me all the way to average, but sum/count will work once I figure out the reference part.

Mark S.

unread,
Jan 2, 2020, 3:04:11 PM1/2/20
to TiddlyWiki
Unfortunately, things aren't always that easy. One of the problems with TW list filters is that they sometimes produce results one at a time, rather than as a title list. Many of the convenient operators (like sum) that we would like to use, on the other hand, often want to see a title list. In this case, you can't grab all the values out of a data tiddler -- you have to enumerate them one by one, and then pack them into something that can be turned back into a title list. The trick here is to do the enumeration in a macro, wikify that macro, and then split the results. If that seems like a lot of extra steps, it's only because ... it is.

Anyway, this seems to work.

Good luck!


\define sumlist() <$list filter="[all[current]indexes[]]" variable="index">{{{ [all[current]getindex<index>] }}},</$list>
 
<table>
<th>Idea</th><th>Vote Count</th><th>Sum</th><th>Avg Score</th>

<$list filter="[tag<currentTiddler>]">
<tr>
<td>{{!!title}}</td>
<$list filter="[all[current]indexes[]count[]]" variable="count">
<$wikify name="sumlist" text="""<<sumlist>>""">
<$list filter="[<sumlist>split[,]sum[]]" variable="sum">
<$list filter="[<sum>divide<count>]" variable="avg">
<td><<count>></td>
<td><<sum>></td>
<td><<avg>></td>
</$list>
</
$list>
</$wikify>
</
$list></tr>
</$list>
</
table>


Stobot

unread,
Jan 2, 2020, 5:14:02 PM1/2/20
to TiddlyWiki
Thanks! Brilliant solution, hadn't thought of that.

It seems like most Dictionary implementations in other languages would have a mirror to the indexes[] operator like a values[]; would it make sense to request that as a future enhancement?

Mark S.

unread,
Jan 2, 2020, 7:02:50 PM1/2/20
to TiddlyWiki
It would make sense to me ;-) . It wouldn't hurt to ask.

I think that the data dictionary wasn't really meant as the go-to data solution in TW. It's better to think in terms of list widgets, filters, and tiddlers. Most of the available tools centre around those features. Albeit, that sometimes means your data isn't very compact.

Good luck!

Mohammad

unread,
Jan 2, 2020, 11:57:06 PM1/2/20
to TiddlyWiki
Another smart solution!
Thanks Mark.
I just propose to add a fixed[n] like fixed[2] to fix number digits on average!

-Mohammad

Mohammad

unread,
Jan 2, 2020, 11:57:38 PM1/2/20
to TiddlyWiki
Added to TW-Scripts  (will be available in next update)


On Thursday, January 2, 2020 at 11:34:11 PM UTC+3:30, Mark S. wrote:
Reply all
Reply to author
Forward
0 new messages