Filter with if then

Hi,

i want to use a filter to go through a list of tiddlers and get a field value if exists and if not get another field value

i dont know if i am thinking correctly or not , or if i am a bit confused by the syntax ,but i am thinking


<$list filter"[!is[system]has[field1]then , get[field1] else,get[field2]

but not sure how to put this together correctly

One way to do this is to use the cascade filter run prefix:

\define try() [get[field1]] [get[field2]] [[there is no field 1 or field2]]

{{{[tag[test]]:cascade[<try>]}}}

The first matching filter with a non empty output will be selected.

3 Likes

Another solution, with the :map filter run prefix:

<$list filter="[tag[Tests]] :map[<currentTiddler>has[f1]then{!!f1}else{!!f2}]">

You can replace [tag[Tests]] with whatever first level selection filter you like (in your initial code it would be [!is[system]]).

@Theophile I like your solution! Nice!

Fred

4 Likes

And yet another solution with functions

\function try.me() [get[field1]] [get[field2]] [[there is no field 1 or field2]]

<<try.me>>

{{{ [tag[test]try.me[]] }}}

Unless you use join in a function, only the first result is returned. With a period in the name it can be used as a Custom operator.

1 Like

Oh, Just a reminder there are new features in 5.3.2 on the way to address if then. I am undertaking a review of it here Peeking at the pre-release 5.3.2 - Conditional Shortcut Syntax

2 Likes

Hi ,

sorry for the late reply, i went with the filter methods as i specifically wanted to use a filter in my use case

thank you very much for your help