List Fields : How to get the "Position" of an item in a list field

186 views
Skip to first unread message

Mohamed Amin

unread,
Mar 23, 2021, 5:57:48 AM3/23/21
to TiddlyWiki
Hi,

Is there a way to get the position of an item in a "list field"?

More info:
Recently I've discovered the "nth" filter operator, and one of the use case I immediately think about was to "iterate" through items in multiple related "list fields"

Take the following example (not a real one, just to get the idea clear):
If a family needs to manage its friends, each family member's Tiddler will have the following fields:
1- friend-names : "list field" contains ALL friends
2- friendships-start-date : "list field" where the "position" of the date determine the friend's names
3- friendships-meter : "list field" where the "position" of the % determine the friend's names

Ex.
friend-names :         [[alex]]          [[andy]]            [[john]]             [[evan]]
friendships-start-date :  [[Mar2015]]   [[Dec2017]]    [[Nov2016]]     [[Feb2011]]
friendships-meter     [[70]]              [[30]]               [[60]]                [[90]]

Now I can use the filter "[contain:friend-name[alex]]" to get all family members who is a friend with "alex".
And if I know the <<position>> of "alex" in the "friend-names" field, I can get the rest of the data corresponding to it for the currentTiddler using a filter like:
"[all[current]get[friendships-meter]enlist-input[]nth<position>]"

Regards

Mat

unread,
Mar 23, 2021, 6:24:41 AM3/23/21
to TiddlyWiki
You can use this to find the position of an item in a list

+[allbefore<item>count[]add[1]]

e.g 

{{{ one two three four five +[allbefore[four]count[]add[1]]  }}} ----> 4

Not sure it's the optimal solution, I just made it up.

<:-)
 


TW Tones

unread,
Mar 23, 2021, 6:34:00 AM3/23/21
to TiddlyWiki
Mats suggestion is one I use.

Another trick is to use count to get the total number T of items then use a range operator from 1 to N, in which the current tiddler will be the item number.

However you then need to retrieve the nth[N] item from the list.

Regards
Tones

Mohamed Amin

unread,
Mar 23, 2021, 7:16:08 AM3/23/21
to TiddlyWiki
Thanks @Mat this is a clever workaround solution.

@Tones, thanks for your reply, but I couldn't get your idea, how can I use "range operator" to calculate the number "N" that I'll use in the "nth[N]" operator?

Mohamed Amin

unread,
Mar 23, 2021, 7:30:32 AM3/23/21
to TiddlyWiki
For future reference: I found the following related:
  1. GitHub issue : Provide an index value in lists #3384 (also there're many other related issues)
  2. Macro by tobibeer

TW Tones

unread,
Apr 4, 2021, 1:38:09 AM4/4/21
to TiddlyWiki
Mohammadd,

I just saw this now. The Count operator or count widget, with an allbefore can be used to count the items before a position in a list to get the N for the Nth.

[{!!list}allbefore[string]count[]] or something similar. add[1] if needed.

Tones

Adithya B

unread,
Apr 5, 2021, 1:12:44 AM4/5/21
to TiddlyWiki
A related question:

Is there any existing solution to take user input and put a list of tiddlers into a field? Something with dropdown/autocomplete like the tag-picker dialog?
Reply all
Reply to author
Forward
0 new messages