Set value of a variable conditionally

118 views
Skip to first unread message

Tony K

unread,
May 20, 2020, 12:19:59 PM5/20/20
to TiddlyWiki
I can't figure out how to set the value of a variable based on a specific condition so, instead, I am writing the code twice

e.g.

<$list filter="[<variable>length[]match[9]]">
<$set name="type" value="short">
{full code here}
</$set>
</$list>
<$list filter="[
<variable>length[]!match[9]]">
<$set name="type" value="long">
{full code here}
</$set>
</$list>

this is not really efficient. 

I tried conditionally inserting the first line of the "$set" but it didnt' work (as follows) even thought logically it should 

<$list filter="[<variable>length[]match[9]]">
<$set name="type" value="short">
</$list>

<$list filter="[
<variable>length[]!match[9]]">
<$set name="type" value="long">
</$list>

{full code here}
</$set>


I am sure there is some kind of a workaround for this

thanks for shedding any light 

Mark S.

unread,
May 20, 2020, 12:26:41 PM5/20/20
to TiddlyWiki
The list widget has an emptyMessage attribute. You can point this to a macro where alternative actions occur when the main filter fails (is empty).

In your second set of widgets, you have unmatched tags/tag endings which is always a bad thing.

Good luck!

Saq Imtiaz

unread,
May 20, 2020, 12:28:11 PM5/20/20
to TiddlyWiki
Many combinations of list and set widgets can be used, but this basic idea is probably what you need:

[<variable>length[]match[9]then[short]else[long]]

Saq Imtiaz

unread,
May 20, 2020, 12:29:47 PM5/20/20
to TiddlyWiki
Eg:

<$list filter="""[<variable>length[]match[9]then[short]else[long]]""" variable="type"/>

{full code here}

</$list>

Tony K

unread,
May 20, 2020, 12:32:40 PM5/20/20
to TiddlyWiki
I did play around with empty message but it gives an empty message and doesn't set a value to a variable (at least from my tests)

Tony K

unread,
May 20, 2020, 12:33:12 PM5/20/20
to TiddlyWiki
Saq does it again :) :) thank you I shld have asked ages ago

Cheers

TonyM

unread,
May 20, 2020, 9:44:02 PM5/20/20
to TiddlyWiki
TonyK

An empty message can be a macro, that contains many things, 

\define onemptymessage()
{empty message code here}
\end

<$list filter="""[<variable>length[]match[9]then[short]else[long]]""" variable="type" emptyMessage=<<onemptymessage>> />

{full code here}

</$list>


It can even look like this

<$list filter="""[<variable>length[]match[9]then[short]else[long]]""" variable="type"
emptyMessage="""
{empty message code here}

"""/>

{full code here}

</$list>

Extending Saq suggestion; I expect this to work (untested today)

[<variable>length[]match[9]then<shortval>else<longval>]

The only limitation being macros cant be passed variables, complex macrs may need their result wikified before using in a filter

Remember as well is all you want to know is is it long or short this will do

{{{[<variable>length[]match[9]then[short]else[long]]}}


Regards
Tony

Tony K

unread,
May 21, 2020, 12:26:25 AM5/21/20
to TiddlyWiki
Hello Tony

Your patience is remarkable thank you so much

yes I am aware that an empty messaeg can be a macro however I wasn't able to make it set the value of the same variable for the rest of the code like explained by Saq and yourself in the if-then-else scenario

the second issue I faced is the <longeval> cannot take arguments which turned out to be documented

TonyM

unread,
May 21, 2020, 1:01:25 AM5/21/20
to TiddlyWiki
Tony K

Ok, I do not understand the original question then however perhaps these could help?
  • Have you tried to use set, its filter and emptyValue which should remain until the closing set?
  • Can you ask the question in a different order?
  • Or can you generate a subset of variables from a list such as "short long N(for length)" then have additional nested lists to interogate these and respond to each differently.
    In this case emptyMessge/value is never triggerd, the result has more testing done to it. eg if not short not long
Regards
TonyM

Mark S.

unread,
May 21, 2020, 10:34:15 AM5/21/20
to TiddlyWiki
But you can set a variable inside a macro:

\define else()
<$set name="type" value="long">
<<type>>
</$set>
\end
<$vars variable="variable">
<$list filter="[<variable>length[]match[9]]" emptyMessage=<<else>>>

<$set name="type" value="short">
{full code here 1}
</
$set>
</$list>
</
$vars>

All that matters is that the alternate "else" does whatever task you want. The scope of all variables in TW is limited -- allways between two tags. Here a variable is set inside the emptyMessage, and then displayed so you an see that it was set and used.

On Wednesday, May 20, 2020 at 9:19:59 AM UTC-7, Tony K wrote:

Tony K

unread,
May 22, 2020, 2:08:39 AM5/22/20
to TiddlyWiki
Hi Mark

in your below example type will be equal to "long" online inside else()  however inside the section {full code here 1} it won't actually the section {cull code here 1} won't even be reached
 
Please tell me if I am mistaken 

Mark S.

unread,
May 22, 2020, 11:35:49 AM5/22/20
to TiddlyWiki
Try ...

<$vars variable="12345678">
<$set filter="[
<variable>length[]compare:number:gteq[9]else[short]] [<variable>length[]compare:number:lt[9]else[long]] +[sort[]last[]]"  name=type select=0 >
<
<type>>
</$set>
</$vars>



On Wednesday, May 20, 2020 at 9:19:59 AM UTC-7, Tony K wrote:
Reply all
Reply to author
Forward
0 new messages