Count entries in fields

72 views
Skip to first unread message

Mohammad

unread,
Oct 22, 2019, 7:51:08 AM10/22/19
to TiddlyWiki
This may be a repeated question

Assume you have few tiddlers some have  field called car

Then you want to count how many cars of each makers is referenced

Example
 Tid01
   car: Toyota
 Tid02
   car: Fiat
 Tid03
   car: BMW
 Tid04
   car: Toyota
 Tid05
   car: BMW
 Tid06
   car: Ford
 Tid07
   car: Toyota
 Tid08
   car: BMW
 Tid09
   car: Ford
 Tid10
   car: Toyota

Answer should be
BMW: 3
Fiat: 1
Ford: 2
Toyota: 4


Mark S.

unread,
Oct 22, 2019, 10:47:51 AM10/22/19
to TiddlyWiki
<$list filter="[each[car]get[car]]">
<<currentTiddler>>: <$count filter="[car<currentTiddler>]"/><br/>
</$list>

Mohammad

unread,
Oct 22, 2019, 11:21:33 AM10/22/19
to TiddlyWiki
Hi Mark,
 Thanks it works!
I assume the input is all[tiddlers+shadows].

One more question if in a field there is Toyota and BMW
Then how modify the above code to cover such a case?

--Mohammad

Mark S.

unread,
Oct 22, 2019, 12:39:42 PM10/22/19
to TiddlyWiki
At this point it is more complicated, and we need to consider the structure of the data. Will the field
literally contain "Toyota and BMW.", in which case the car with the highest count will probably
be the "and" car. Or will it be a list field of some type? Or will we know the names of car makes in
advance?

Mohammad

unread,
Oct 22, 2019, 2:24:23 PM10/22/19
to TiddlyWiki
Well yes,
 there is no and! The field contains only the name of car makers so the correct values are like these
 Toyota BMW
 Ford Toyota

but we don't know their name in advance.

Mark S.

unread,
Oct 22, 2019, 2:51:07 PM10/22/19
to TiddlyWiki
As long as the car names do not have spaces, this works:

<$list filter="[get[car]split[ ]each:value[]sort[]]" variable="car">
<
<car>>: <$count filter="[all[tiddlers]contains:car<car>]"/><br/>
</$list>


Mohammad

unread,
Oct 22, 2019, 3:27:13 PM10/22/19
to TiddlyWiki
Thanks Mark!
This works for most of cases!

Cheers
Mohammad
Reply all
Reply to author
Forward
0 new messages