Count fields that have a specific value within one single tiddler

107 views
Skip to first unread message

Mirko Richter

unread,
Aug 30, 2019, 5:03:32 AM8/30/19
to TiddlyWiki
Hi,

is it possible to count all fields within some single specific tiddler that have a certain value? If yes, how ;)? Im currently struggling on that and don't know how to go on...

(My use case: a filled-out questionnaire with the text "wrong" or "correct" (i already calculate it while stepping the wizzard) in an arbitrary number of fields. The field's name is the name of the underlying question. I just want to know how many "wrong"s or "correct"s are in there.)

Additional question: Can i also restricted the fields under consideration in terms of regexp[], suffix[], prefix[] or other string-based criteria?

Thanks,
Mirko

Anjar

unread,
Aug 31, 2019, 6:47:23 AM8/31/19
to tiddl...@googlegroups.com
Hi Mirko

That's a great question, and I guess there should be simpler solutions, but as far as I could see, this will work:

\define getScoreTiddler() $:/score/{{!!title}}

\define countScore()
<$action-deletetiddler $filter="[prefix[$:/temp]tag[ScoreTiddler]tag[$(scoreTiddler)$]]"/>
<$list filter="[all[current]fields[]] -text -title" variable=F >
<$list filter="[all[current]get<F>match[correct]]" >
<$action-createtiddler $basetitle="$:/temp" tags="ScoreTiddler [[$(scoreTiddler)$]]"/>
</$list>
</$list>
<$set name=c filter="[prefix[$:/temp]tag[ScoreTiddler]tag[$(scoreTiddler)$]count[]]">
<$action-setfield $tiddler="$(scoreTiddler)$" text=<<c>> />
</$set>
\end

<$wikify name=scoreTiddler text=<<getScoreTiddler>> >
<$button actions=<<countScore>> >
Count score
</$button>

Score: <$transclude tiddler=<<scoreTiddler>> />

</$wikify>

It creates a temporary tiddler for each correct answer, then counts the tiddlers and assigns the number to a new tiddler "$:/score/<title of question>" so that the score for each tiddler is saved separately.

Note that you have to click a button (twice it seems?) to (re-)calculate the score

Edit: Had to add an extra tag to the temp-tiddlers for them not to interfere with other questions


Best,
Anders

Mark S.

unread,
Aug 31, 2019, 3:07:50 PM8/31/19
to TiddlyWiki
Returns count for all fields that start with "stuf" and contain string "wrong".

The complexity of this answer suggests that you might want to restructure your project/questions, so that each response goes into
its own tiddler. Then getting the count (and other data analyses) will be much more straightforward.

\define mywrongs()
<$vars mytid="""$(mytid)$"""><$list filter="[<mytid>fields[]regexp[^stuf]]" variable="field"><$list filter="[<mytid>get<field>regexp[wrong]]"><<field>>
</$list></$list></$vars>
\end
\define wrongsintiddler(mytid)
<$vars mytid="""$mytid$""">
<$wikify text=<<mywrongs>> name="wrongs">
{{{ [enlist<wrongs>count[]] }}}
</$wikify>
</
$vars>
\end

<<wrongsintiddler "regexp Operator">>


Mirko Richter

unread,
Sep 5, 2019, 1:43:59 AM9/5/19
to TiddlyWiki
Hi Mark,

thanks for that one! I consider splitting it to more "tiddler-style" soon.

Best wishes,
Mirko
Reply all
Reply to author
Forward
0 new messages