problem comparing numeric values

55 views
Skip to first unread message

Jean-Pierre Rivière

unread,
Oct 14, 2020, 5:09:57 AM10/14/20
to TiddlyWiki
I have come upon something I can't fix relaticve to comparing value.

First, I tested my filter this way and all was OK:

\define target() 2
target=<<target>><br/>
<$set name="qty" value=<<target>>>
<<.operator-example 6 "[<qty>compare:number:gt[1]then[several]else[single]]">>
<<.operator-example 7 "[<target>compare:number:gt[1]then[several]else[single]]">>
<$set name="loc" filter="[<qty>compare:number:gt[1]then[several]else[single]]">
got <<qty>>: <<loc>><br/>
</$set>
</$set>

"several" was answered in each case. All very fine. Then I translated it in my wiki for application.

\define countTaggedBySelf() <$count filter="[all[current]tagging[]butfirst[]]"/>
<$set name="qty" value=<<countTaggedBySelf>> >
<$set name="loc" filter="[<qty>compare:number:gt[1]then[several]else[single]]">
got <<qty>>: <<loc>><br/>
</$set></$set>

Then I get "got 2: single" ??? (in a tiddler where the countTaggedBySelf has sense and returns 2.)

But if I write instead
<$set name="qty" value=2 >

then again all is fine and I get "got 2: several"

Where is my error?

Eric Shulman

unread,
Oct 14, 2020, 5:55:03 AM10/14/20
to tiddl...@googlegroups.com
On Wednesday, October 14, 2020 at 2:09:57 AM UTC-7, Jean-Pierre Rivière wrote:
\define countTaggedBySelf() <$count filter="[all[current]tagging[]butfirst[]]"/>
<$set name="qty" value=<<countTaggedBySelf>> >
<$set name="loc" filter="[<qty>compare:number:gt[1]then[several]else[single]]">
got <<qty>>: <<loc>><br/>
</$set></$set>

Then I get "got 2: single" ??? (in a tiddler where the countTaggedBySelf has sense and returns 2.)
But if I write instead
<$set name="qty" value=2 >

Macros do *not* "evaluate the syntax and return the output"... they just do substitutions for $param$ and $(variable)$

Thus, countTaggedBySelf() does not return a number, it returns: <$count filter="[all[current]tagging[]butfirst[]]"/>
and the subsequent <$set name="qty" ...> also just sets the qty variable to: <$count filter="[all[current]tagging[]butfirst[]]"/>

There are several ways to fix this:

Method #1: use <$wikify> to convert the <$count> into it's actual number
\define countTaggedBySelf() <$count filter="[all[current]tagging[]butfirst[]]"/>
<$wikify name="qty" text=<<countTaggedBySelf>> >

<$set name="loc" filter="[<qty>compare:number:gt[1]then[several]else[single]]">
got
<<qty>>: <<loc>><br/>
</$set></$wikify>

Method #2: use the [count[]] filter instead of a macro that invokes the $count widget
<$set name="qty" filter="[all[current]tagging[]butfirst[]count[]]" >
<$set name="loc" filter="[
<qty>compare:number:gt[1]then[several]else[single]]">
got <
<qty>>: <<loc>><br/>
</$set></$set>

Simplification #1: use $vars with "filtered transclusion" instead of $set with filter param
<$vars qty={{{ [all[current]tagging[]butfirst[]count[]] }}}>
<$vars loc={{{ [
<qty>compare:number:gt[1]then[several]else[single]] }}}>
got <
<qty>>: <<loc>><br/>
</$vars></$vars>

Simplification #2: if you don't need the "qty" value, combine all the filter syntax
<$vars loc={{{ [all[current]tagging[]butfirst[]count[]compare:number:gt[1]then[several]else[single]] }}}>
got <
<loc>><br/>
</$vars>

Simplification #3: if you just want to display the result, skip the $vars entirely:
{{{ [all[current]tagging[]butfirst[]count[]compare:number:gt[1]then[several]else[single]] }}}

enjoy,
-e

TW Tones

unread,
Oct 14, 2020, 6:29:31 AM10/14/20
to TiddlyWiki
Eric,

Nice to see your use of vars and filtered transclusions here, I really dislike the need to wikify, It took me ages to work out when I must and when not. Your approach seems a nice alternative.

If however some one is using wikify the name is reusable, and a little easier to read.

\define qty() <$count filter="[all[current]tagging[]butfirst[]]"/>
<$wikify name="qty" text=<<qty>> >


<$set name="loc" filter="[<qty>compare:number:gt[1]then[several]else[single]]">
got
<<qty>>: <<loc>><br/>
</$set></$wikify>

Just a simplification of one the of the forms.

Tones

Jean-Pierre Rivière

unread,
Oct 14, 2020, 6:37:15 AM10/14/20
to TiddlyWiki
Thank you Eric. Once again, I stumble upon the terrible rule "macro do only text substitution". I know it, but I have not yet realized all what it means. I selectred the wikify approach of method #1 as I want to keep using the countTaggedBySelf macro to avoid duplicating code.

Simplification #3 creates a link, which is not wanted. Is there a similar quick notation to just get the raw text instead of an internal link? Note that is whay I created my macro with the count widget inside.

Regards,

Le mercredi 14 octobre 2020 à 11:55:03 UTC+2, Eric Shulman a écrit :
On Wednesday, October 14, 2020 at 2:09:57 AM UTC-7, Jean-Pierre Rivière wrote:
\define countTaggedBySelf() <$count filter="[all[current]tagging[]butfirst[]]"/>
<$set name="qty" value=<<countTaggedBySelf>> >
<$set name="loc" filter="[<qty>compare:number:gt[1]then[several]else[single]]">
got <<qty>>: <<loc>><br/>
</$set></$set>

Then I get "got 2: single" ??? (in a tiddler where the countTaggedBySelf has sense and returns 2.)
But if I write instead
<$set name="qty" value=2 >

Macros do *not* "evaluate the syntax and return the output"... they just do substitutions for $param$ and $(variable)$

Thus, countTaggedBySelf() does not return a number, it returns: <$count filter="[all[current]tagging[]butfirst[]]"/>
and the subsequent <$set name="qty" ...> also just sets the qty variable to: <$count filter="[all[current]tagging[]butfirst[]]"/>

There are several ways to fix this:

Method #1: use <$wikify> to convert the <$count> into it's actual number:
\define countTaggedBySelf() <$count filter="[all[current]tagging[]butfirst[]]"/>
<$wikify name="qty" text=<<countTaggedBySelf>> >

<$set name="loc" filter="[<qty>compare:number:gt[1]then[several]else[single]]">
got
<<qty>>: <<loc>><br/>
</$set></$wikify>

Method #2: use the [count[]] filter instead of the $count widget:
<$set name="qty" filter="[all[current]tagging[]butfirst[]count[]]" >
<$set name="loc" filter="[
<qty>compare:number:gt[1]then[several]else[single]]">
got <
<qty>>: <<loc>><br/>
</$set></$set>

Simplification #1: if you don't need the "qty" value, combine all the filter syntax
<$set name="loc" filter="[all[current]tagging[]butfirst[]count[]compare:number:gt[1]then[several]else[single]]">
got <
<loc>><br/>
</$set>

Simplification #2: use $vars with a "filtered transclusion" instead of $set
<$vars loc={{{ [all[current]tagging[]butfirst[]count[]compare:number:gt[1]then[several]else[single]] }}}>
got <
<loc>><br/>
</$vars>

Simplification #3: if you just want to display the result, skip the $vars (or $set) entirely:

Eric Shulman

unread,
Oct 14, 2020, 7:01:27 AM10/14/20
to TiddlyWiki
On Wednesday, October 14, 2020 at 3:37:15 AM UTC-7, Jean-Pierre Rivière wrote:
Thank you Eric. Once again, I stumble upon the terrible rule "macro do only text substitution". I know it, but I have not yet realized all what it means. I selected the wikify approach of method #1 as I want to keep using the countTaggedBySelf macro to avoid duplicating code.

Simplification #3 creates a link, which is not wanted. Is there a similar quick notation to just get the raw text instead of an internal link? Note that is why I created my macro with the count widget inside.

Use the <$text> widget, like this:
<$text text={{{ [all[current]tagging[]butfirst[]count[]compare:number:gt[1]then[several]else[single]] }}}/>

-e
 

TW Tones

unread,
Oct 14, 2020, 8:31:38 AM10/14/20
to TiddlyWiki
Eric,

I stumbled on two things recently that may interest you. Although I dare say you know they exist;

They can may writing wikitext and macros much easier.


The First - Using templates with filtered transclusions
Consider the filtered transclusion in this thread
{{{ [all[current]tagging[]butfirst[]count[]compare:number:gt[1]then[several]else[single]] }}}

And displaying it in text
<$text text={{{ [all[current]tagging[]butfirst[]count[]compare:number:gt[1]then[several]else[single]] }}}/>

I have started using short template names, so in a tiddler called (text) it contains;
<$text text=<<currentTiddler>>/>

So now we can get the filtered transclusion to return it in text form as follows
{{{ [all[current]tagging[]butfirst[]count[]compare:number:gt[1]then[several]else[single]]  || (text) }}}

I have taken this lot further for a range of situations and created short tiddlers of the form "(name)" to be used as templates.
  • Do you see any problems with this approach?
  • I don't mind they come up in searches, although we could filter them out, it is actually convenient to find them.
  • They all operate in currentTiddler and can this be placed inside lists, as parameters and a lot more.
I will share my current list if you are interested.

The Second is using transclusions as concatenation
  • In wiki text we can freely concatenate all sorts of items for example "{{!!fieldname}}{{{ filter }}}<<varname>>even<<macroname parameter>> works"
  • But to write macros or be forced to wikify makes concatenation can be quite complicated.
  • However if you put this in a tiddler and transclude it is done for you
  • However this means using more than one tiddler, OK for reusable concatenations, but not for ad hoc ones.
  • Yet you can use a field on the current tiddler to do this "concatenation" by wiki text and simply transclude eg use {{!!working}} when the working field contains
    {{!!fieldname}}{{{ filter }}}<<varname>>even<<macroname parameter>>works

For example place this in a New tiddler on TiddlyWiki.com 
\define macroname(param) The Macro Output <<transclusion>> with $param$

{{!!working}}+

Then add fieldname with a value

Add the working field containing
{{!!fieldname}}{{{ [<currentTiddler>removeprefix[New]] }}}<<transclusion>>even<<macroname parameter>> works

and see this in action for yourself.

Once again for this original thread we can place the filtered transclusion in the working field

All that remains is to find a way to ensure the result from a working field is text only when needed.



Regards
Tones
Reply all
Reply to author
Forward
0 new messages