How to loop through Tiddlers of a certain Tag and gather up all the values in a certain field

122 views
Skip to first unread message

David

unread,
Apr 6, 2020, 11:37:48 AM4/6/20
to TiddlyWiki
I have a bunch of tiddlers with the tag of "Task" and a field called "context"

I'd like to loop over them, or whatever, and end up with a variable or tiddler that is a list of all the different values found in that "context" field.

I then plan to loop over that and show some stuff.  Right now, that list is static and manually created by me.

I don't know if there is a command to do it efficiently, or if a loop is necessary.

For example... if tiddlers look like...

Name/Title    | Context (Field)
--------------|--------------------------------------
Clean Gutters | Home
Wash Car      | Home
Call School   | Work
Buy bolts     | Out


Then it should save "Home Work Out" into a variable or tiddler.  I guess variable would be best, if possible.

Here is some code to loop over the tag in question, and this transclude command outputs the context, but of course, there are repeats (which I don't want).  And of course, this doesn't save it to a variable.  But maybe it's a good start?

<$list filter="[tag[Task]]" variable="item">
 <$transclude tiddler=<
<item>> field="context" />
</$list>


Mark S.

unread,
Apr 6, 2020, 1:23:30 PM4/6/20
to TiddlyWiki
This finds your unique contexts, puts them in the variable "item", and lists them:

<$list filter="[tag[Task]each[context]get[context]]" variable="item">
 <<item>> <br/>
</$list>

The "each" operator removes duplicates. The "get" operator gets the value. I've never really understood why "each" comes before "get", but that is the recipe you find at tiddlywiki.com.

HTH

Saq Imtiaz

unread,
Apr 6, 2020, 3:45:10 PM4/6/20
to TiddlyWiki
This is my understanding of how the each filter is used:

I think of each as "group by operand". So for each value of context it reduces the list to one tiddler with that value, from which you can then get the value of context. 

If you reduce to a single field with get first, you can't group as all you have now is a list of contexts. You could then probably do something like this:
[tag[Task]get[context]unique[]]

Mohammad

unread,
Apr 6, 2020, 3:46:11 PM4/6/20
to TiddlyWiki
This kind of job can be done very easily using

TiddlyTables

or 

Shiraz (its dynamic table)

Both are pure wikitext plugins NO JavaScript, NO overwriting core tiddlers!

Safe and robust!

--Mohammad

Mohammad

unread,
Apr 6, 2020, 3:53:11 PM4/6/20
to TiddlyWiki
Hi Saq,

Is unique a new filter operator?
It works and it is a semantic one!

--Mohammad

Saq Imtiaz

unread,
Apr 6, 2020, 4:37:10 PM4/6/20
to TiddlyWiki
Not sure when unique[] was added. I read code more often than documentation when it comes to TW, so I hadn't realized that it wasn't documented on tiddlywiki.com

TonyM

unread,
Apr 6, 2020, 5:52:00 PM4/6/20
to TiddlyWiki
Mark

If it helps this how I conceptualise the each operator working
[tag[Task]each[context]get[context]]

  • [tag[TaskGet a list of all tiddlers with the tag task
  • each[contextFor all these tiddlers that has the context field, get a tiddler title of a tiddler, that represents each unique value in the context field.
  • get[contextSince each title generated has a unique context using get will return that value in the context field.

You could say that "each" returns a / any, "but only one" tiddler title, of each value in the "context".

Each is a great way to get all previously used values in a field (after deduplication)

I like it when others share their "conceptual" understanding, so thought I would do so for this one.

Hope it helps

Regards
Tony

TonyM

unread,
Apr 6, 2020, 5:57:21 PM4/6/20
to TiddlyWiki
Perhaps this "unique" is a hidden operator

However if it is not documented it may be because it contains complexities or a bug such that it was not documented.

Regards
Tony

Saq Imtiaz

unread,
Apr 6, 2020, 6:15:47 PM4/6/20
to TiddlyWiki
Tony: I see nothing in the code that would make me hesitant about using the unique filter.

TonyM

unread,
Apr 6, 2020, 7:59:57 PM4/6/20
to TiddlyWiki
Saq,

That is good. Perhaps open an issue that it is not documented.

Or perhaps you could document it?

Regards
Tony

TonyM

unread,
Apr 6, 2020, 8:59:32 PM4/6/20
to TiddlyWiki
Post script

We now also have the Maths operators see https://tiddlywiki.com/#Mathematics%20Operators and https://tiddlywiki.com/#Dominant%20Append which allows us to bypass the dominant append

I imagine Unique can be used alongside these.

Regards
Tony

Mohammad

unread,
Apr 7, 2020, 1:28:34 AM4/7/20
to TiddlyWiki
Added to TW-Scripts!


On Tuesday, April 7, 2020 at 12:15:10 AM UTC+4:30, Saq Imtiaz wrote:

Thomas Stone

unread,
Apr 7, 2020, 5:58:02 AM4/7/20
to TiddlyWiki
Might I recommend you create a separate tag each context, instead of a field. A tag itself can be a tiddler with its own tag.

So you could create Home, Work, and Out tiddlers, each with a Task tag. Then your filter would look like: [tag[Task]!is[tag]tags[]tag[Task]unique[]]

Mat

unread,
Apr 7, 2020, 5:59:24 AM4/7/20
to TiddlyWiki
@David

IMO the other fellas here have misunderstood your request. It is really not a difficult problem ...OR I'm the one who misunderstands.

To collect contexts into a variable, you use:

<$set name=foo filter="[tag[Task]get[context]]">
...
</$set>


Then do whatever you want (you didn't specify) instead of the "..."

<:-)

David

unread,
Apr 7, 2020, 11:24:15 AM4/7/20
to tiddl...@googlegroups.com
No, you understood it.  However, your code has a lot of dupes.  That's one thing I was hoping to find out how to fix.  But putting in the "unique" bit mentioned above, seems to have worked.  The filter is now:

[tag[Task]get[context]unique[]sort[]]

And it works great

Thanks so much Everyone!

David

unread,
Apr 7, 2020, 11:51:52 AM4/7/20
to TiddlyWiki
Oh, but the page now doesn't refresh when I create a new tiddler with the right context.  If I make sur eeverything is saved and then reload the page in the browser, the new tiddler will show up.  But it doesn't seem to happen on the fly like it used to?

Maybe too much thinking now?

Is there a way to make it automatically "re-poll" or soemthing like that?

But even if this isn't fixed, I'm happy enough just refreshing.

Mat

unread,
Apr 7, 2020, 11:58:25 AM4/7/20
to TiddlyWiki
I was surprised to learn that, indeed, it doesn't filter out duplicates. I learn something new every day. Good you got what you wanted anyway.

Refreshing... that'd be another thing I did not expect then but I can't reproduce it. When I try it, it even refreshes while I'm still editing (another) tiddlers context field and also when I'm using a button to set the field. Could you show your code that doesn't work?

<:-)
Reply all
Reply to author
Forward
0 new messages