position in a list

81 views
Skip to first unread message

Mohammad

unread,
Aug 2, 2019, 8:29:10 AM8/2/19
to TiddlyWiki
I have a list of tiddlers, I iterate over the list and I want to know if an item is before or after the selected item!

How to do this?


--Mohammad

Mohammad

unread,
Aug 2, 2019, 8:41:33 AM8/2/19
to TiddlyWiki
A partial solution

<$set name=selected value=7>
<$list filter="[range[1,10]]"  variable="item" >
<$list filter="[range[1,10]allbefore<selected>] +[prefix<item>]">
<<currentTiddler>>  is before selected item: <<selected>><br>
</$list>
</$list>
</$set>



This works, any better idea? Any TW intrinsic solution?


--Mohammad

Mat

unread,
Aug 2, 2019, 8:41:51 AM8/2/19
to TiddlyWiki
Maybe you can treat the list like a string and cut the list at the selected item (listbefore[]) and then see if the item is in the remaining list - if it is not, then it was before otherwise it was after.

<:-)

TonyM

unread,
Aug 2, 2019, 9:15:39 AM8/2/19
to TiddlyWiki
Mohammad

This is not the full answer but counting the items in a list can give you helpful information. The set widgets select parameter and the rest operator amongst others all provide the components to do this and do it well.

Perhaps store the number of items from count then use the select parameter to iterate each item. The position in the list will then be a number and last time I looked 4 was after 3, and 3 before 4 so the relation ship is obviouse

Tony

Mark S.

unread,
Aug 2, 2019, 1:12:50 PM8/2/19
to TiddlyWiki
<$set name=selected value=7>
<$list filter="[range[1,10]]"  variable="item" >
<$list filter="[range[1,10]allbefore<selected>] -[range[1,10]allbefore<item>] -[range[1,10]allafter<item>]">
<<item>>  is before selected item: <<selected>><br>
</$list>
</$list>
</$set>

Mark S.

unread,
Aug 2, 2019, 4:00:01 PM8/2/19
to TiddlyWiki
I think this answers your question as described in your text -- note that it specifies both numbers to compare. It uses pre20

<$vars selected=7 whereami=8>
<$list filter="[range[1,10]allbefore<selected>match<whereami>]">
<<whereami>>  is before selected item <<selected>><br>
</$list>
<$list filter="[range[1,10]allafter<selected>match<whereami>]">
<<whereami>>  is after selected item <<selected>><br>
</$list>
</$vars>


On Friday, August 2, 2019 at 5:41:33 AM UTC-7, Mohammad wrote:

TonyM

unread,
Aug 2, 2019, 6:46:06 PM8/2/19
to TiddlyWiki
Mohammad,

I would have thought all you need is in the order operators especially
afterfind which input title follows a specified one
beforefind which input title precedes a specified one


I have not used these extensively yet but I believe they hold the key to problems like your own, keep in mind you can use these to establish one set, and subtract that from another set. These should provide all the elements for lists, queues head and tail operations and alot more.

For example
using first then storing rest, then using first on the rest allows you to step through every member and allows you to see what is coming. You could count how many items remain in rest for a progress indicator and more....

Also
The range operator documentation should also be extended to show how to provide values to the <begin>,<end>,<step> parameters from fields and variables.
<$set name=number-or-members value={{{ [filter] +count[]] }}}>
the range[1,number-or-members]

Second section in table is Order Operators, note that N is a number you can provide.

afterfind which input title follows a specified one
beforefind which input title precedes a specified one
bfsame as rest
butfirstsame as rest
butlastdiscard the last N input titles
firstselect the first N input titles
lastselect the last N input titles
limitselect the first or last N input titles!
nextfind which titles in a list field follow the input ones
nsortsort the input by number field!
nsortcssort the input titles by number field, treating upper and lower case as different!
nthselect the Nth input title
orderselectively reverse the input list
previousfind which titles in a list field precede the input ones
restdiscard the first N input titles
reversereverse the order of the input titles
sortsort the input by text field!
sortansort the input by text field considering them as alphanumerics!
sortcssort the input by text field, treating upper and lower case as different

Regards
Tony

Mohammad

unread,
Aug 3, 2019, 3:28:18 AM8/3/19
to TiddlyWiki
Smart solution Mark!

Added to TW-Scripts.
Reply all
Reply to author
Forward
0 new messages