[TW5] Applying nth[] to an explicit list

92 views
Skip to first unread message

FrD

unread,
Aug 11, 2015, 2:00:21 PM8/11/15
to TiddlyWiki
Hi,

I have a macro that returns a list of values, say val1, val2 and val3.
I want to affect these values to the fields of a target tiddler (field1, field2 and field3).
If considered as a list of tiddler's titles, I can use the nth[] operator on the output of the macro.
But I can't find a way with only one button.

A solution with two buttons :
-the first button affects the output of the macro (as a whitespace separated list) to the list field of the current tiddler,

<$button>
<$action-setfield list=<
<macro>>/>
Calculate list
</$button>


- and the second button uses the nth operator on the content of the list field, something like :
<$button>
<$set name="val1" filter="[list[]nth[1]]">
<$set name="val2" filter="[list[]nth[2]]">
<$action-setfield $tiddler="targetTiddler" field1=<
<val1>>W/>
<$action-setfield $tiddler="EssaiBouton"  field2=<
<val2>>/>
Set Fields
</$set>
</$set>
</$button>



So I was wondering if there is a simpler way to acheive the result ?

Thanks

FrD

Jed Carty

unread,
Aug 11, 2015, 3:09:17 PM8/11/15
to TiddlyWiki
Here you should be able to use + in the filter. I don't know how your macro works but if you have some normal filter expression you can do thinks like [tag[bob]]+[nth[1]]

If you are constructing your list differently than you may be able to use a set widget and a macro like this:

\define thisButton()
<$button>some button label
<$set name="val1" filter="""$(TheList)$+[nth[1]]""">
<$set name="val2" filter="""$(TheList)$+nth[2]]""">
<action-setfield list="""$(TheList)$"""/>


<$action-setfield $tiddler="targetTiddler" field1=<<val1>>W/>
<$action-setfield $tiddler="EssaiBouton" field2=<<val2>>/>

</$button>
\end

<$set name=TheList value=<<macro>>>
<<thisButton>>
</$set>

Hopefully that helps

FrD

unread,
Aug 11, 2015, 4:55:44 PM8/11/15
to TiddlyWiki
Hi Jed,

Thanks for answering !
Sorry I can't get anywhere with your proposal, even when I make some modifications.
When thinking about my problem, I came to rephrase it in another way :

How can I apply the nth[] operator to an explicit list of tiddlers titles ? (that was the title of the thread !)

A filter can be an expression like [tag[test]] and I can apply the operator :
[tag[test]]nth[2]
But when the filter is an explicit list of tiddlers like one two three,
or [[one]] [[two]] [[three]], how do I apply the nth operator to such a list (which is not generated by another operator like list[] or tag[]) ?

FrD

FrD

unread,
Aug 12, 2015, 10:08:03 AM8/12/15
to TiddlyWiki
Hi,

I've tried to solve my problem with a new filter which transforms a string of space separated values into something suitable for operators like nth[].
I've called it 'explicit'.
Note that I'm not familiar with the internals of filters !

/*\
title: explicit.js
type: application/javascript
module-type: filteroperator

Filter operator for creating explicit input from string

\*/

(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Export our filter function
*/

exports
.explicit = function(source,operator,options) {
       
var str = operator.operand;
       
var results = str.split(" ");
       
return results;
};

})();

So I can use :

<$set name="test" filter="[explicit[one two three]nth[2]]">
test : <
<test>>
</$set>

The result is :

test : two

But if I try to use a variable it doesn't work any more :

\define myMacro()
one two three
\end

<$set name="test" filter="[explicit[<myMacro>]nth[2]]">
test
with variable : <<test>>
</$set>


So , if someone has a clue  ..
Thanks in advance

FrD





Le mardi 11 août 2015 22:55:44 UTC+2, FrD a écrit :

FrD

unread,
Aug 12, 2015, 1:39:01 PM8/12/15
to TiddlyWiki
Hi,

I've found my error :

filter="[explicit<myMacro>nth[2]]"

instead of :

filter="[explicit[<myMacro>]nth[2]]"

The correct code is below :

\define myMacro()
one two three
\end

<$set name="test" filter="[explicit<myMacro>nth[2]]">
test
with variable : <<test>>
</$set>



FrD

Eric Shulman

unread,
Aug 12, 2015, 2:47:09 PM8/12/15
to TiddlyWiki
On Wednesday, August 12, 2015 at 10:39:01 AM UTC-7, FrD wrote:
\define myMacro()
one two three
\end

<$set name="test" filter="[explicit<myMacro>nth[2]]">
test
with variable : <<test>>
</$set>


This should also work, without needing to define a custom filter:
\define myMacro() one two three
\define myFilter() $(myMacro)$ +[nth[2]]
<$set name="test" filter=<<myFilter>>>

test
with variable : <<test>>
</$set>

enjoy,
-e

FrD

unread,
Aug 12, 2015, 5:00:46 PM8/12/15
to TiddlyWiki
Hi Eric,

Thanks a lot for your answer !
It's much simpler and no need to define a new filter.
I would never have imagined defining such a macro (myFilter) without matching "[".

FrD
Reply all
Reply to author
Forward
0 new messages