Take the list of titles from a user field, iterate and extract another user field and find the max

57 views
Skip to first unread message

Bhilhanan Jeyaram

unread,
Nov 3, 2020, 4:54:31 PM11/3/20
to TiddlyWiki
Hello,

I'm trying to figure out the syntax for the below -

A tiddler "Tiddler 2" has a field "depends-on" with value "[[Tiddler 1]] [[Tiddler 3]]". Each each these two tiddlers has a field "sequence". Let's say "Tiddler 1":sequence=1 and "Tiddler 3":sequence=2. I'm trying to parse the list of "depends-on" and get the max value of "sequence" and then increment that number by 1 and assign that value to "Tiddler 2". In this example "Tiddler 2" will have the "sequence" value assigned as 3

Please help me with figuring out the syntax to achieve this

Thanks,
Bhilhanan Jeyaram

Eric Shulman

unread,
Nov 3, 2020, 5:29:27 PM11/3/20
to TiddlyWiki
On Tuesday, November 3, 2020 at 1:54:31 PM UTC-8, Bhilhanan Jeyaram wrote:
A tiddler "Tiddler 2" has a field "depends-on" with value "[[Tiddler 1]] [[Tiddler 3]]". Each each these two tiddlers has a field "sequence". Let's say "Tiddler 1":sequence=1 and "Tiddler 3":sequence=2. I'm trying to parse the list of "depends-on" and get the max value of "sequence" and then increment that number by 1 and assign that value to "Tiddler 2". In this example "Tiddler 2" will have the "sequence" value assigned as 3

You will need to use a $button widget to modify a tiddler and set a field value.

Place the following into Tiddler 2:

<$button> set sequence
   <$action-setfield sequence={{{ [enlist{!!depends-on}get[sequence]nsort[]last[]add[1]] }}} />
</$button>

Notes:
* enlist{!!depends-on} gets the list of tiddlers that the current tiddler depends on
* get[sequence] gets the sequence field value from each of those tiddlers
* nsort[] does an ascending numeric sort of the sequence numbers
* last[] gets the last number (i.e., the max value)
* add[1] increments that number
* the result of the entire filter (the new number) is then assigned to the sequence field of the current tiddler

Note: you can put the above button into a separate tiddler (e.g., "SetSequenceNumber") and then transclude it using:
{{||SetSequenceNumber}}

Alternatively, you could tag the SetSequenceNumber tiddler with $:/tags/Macro, containing:
\define SetSequenceNumber()
<$button> set sequence
   
<$action-setfield sequence={{{ [enlist{!!depends-on}get[sequence]nsort[]last[]add[1]] }}} />
</$button>
\end
Then, to invoke the macro, you would add the following in "Tiddler 2"
<<SetSequenceNumber>>

enjoy,
-e

Bhilhanan Jeyaram

unread,
Nov 3, 2020, 7:30:14 PM11/3/20
to TiddlyWiki
Thanks a lot Eric Shulman. I was missing the "enlist" and "get" functions.

Based on reading one of the other thread, I realized that Button is the only one that can trigger the action-setfield. Is there a plan in the future where I could change the "sequence" in one Tiddler and all the tiddlers that depend on it recompute its own "sequence" value automatically?

Also, thanks for showing me how to do templates. I can see its benefits and I'm refactoring my wiki to use templates now

Thanks,
Bhilhanan Jeyaram
Reply all
Reply to author
Forward
0 new messages