Struggling with multiple conditions and display expression


(John Doernberg) #1

I have a TB document containing books I’ve read or want to read. I’m trying to write a rule for by book prototype so that the display expression contains the book’s name and the following date information (using a string rather than a date), as appropriate:

  1. If I haven’t read it, it should say “Never” in brackets
  2. If I’ve read it but can’t remember when, it should say “Unknown” in brackets
  3. If I’ve read it and supplied the month and year finished, the month and year in brackets

Here’s the date expression: $Name+(" “)+”["+($WhenFinished)+"]"

Here’s the current rule:
if ($WantToRead|!$HaveRead) {($WhenFinished=“Never”)}; else {if ($MonthYearFinished) {$WhenFinished=$MonthYearFinished} else {$WhenFinished=“Unknown”}};

The result: for books I’ve read but can’t remember when, the display expression is the book’s name and open and closed brackets with nothing inside - rather than “Unknown.”

I’ve tried moving things around and adding/subtracting parentheses and brackets, but haven’t gotten it right yet. Any guidance would be much appreciated.

An additional minor annoyance is that I’d like to disable the display expression for the prototype but not of course for notes using it. I searched the forum and tried various combinations of $RuleDisabled, but haven’t figured that one out either.

Thanks very much.


(eastgate) #2

The result: for books I’ve read but can’t remember when, the display expression is the book’s name and open and closed brackets with nothing inside - rather than “Unknown.”

Syntax Error: The semicolon before the first else is almost certainly unwanted

Advice for mastering problems like this?

First, we can eliminate the whole question of the display expression by asking, not what’s displayed, but what is the value of $WhenFinished? If it’s wrong, the display will be wrong. So let’s focus on that.

General advice: nested conditions are confusing, and they’re easy to get wrong. Let’s see if we can avoid them.

 $WhenFinished=$MonthYearFinished;
 if ($WhenFinished=="") {$WhenFinished="Unknown"};
 if ($WantToRead & (!$HaveRead)) { $WhenFinished="Never" }

Note, too, that you only use $WantToRead and $HaveRead in the first clause of your rule.


(Mark Anderson) #3

First, in your prototype disable the Rule and the DisplayExpression - you don’t want these acting in the prototype.

Next, set this as your rule. I’ve laid it out for clarity to show the nesting but you can safely remove the line breaks an indents if you prefer:

if ($WantToRead|!$HaveRead){
   $WhenFinished="Never";
} else {
   if ($MonthYearFinished) {
      $WhenFinished=$MonthYearFinished
   } else {
      $WhenFinished="Unknown";
   }
}

Now the Display Expression code (again for the prototype):

$Name+" ["+($WhenFinished)+"]"

That gives you a display result like this:

Ulysses [Never]

for the book ‘Ulysses’ that you have not yet read.

Tested in v7.3.0.


(John Doernberg) #4

Thanks very much! Mark Bernstein’s worked. No doubt through some fault of my own, Mark Anderson’s returned “Never” rather than “Unknown” for books I’ve read but can’t remember when.

I think I chickened out by setting the “Finished” attributes as strings rather than dates, as now I’m dealing with unsightly drop-down boxes whenever I start to type in the When Finished box. If I change the “Finished” attributes to dates from strings, do I get the Display Expression I want (of “Month Year”) with this for the Display Expression: $Name+(" “)+”["+$WhenFinished.format(“MM y”)+"]"?

Thanks as always for the great and responsive help.


(Mark Anderson) #5

I’m confused as to the purpose of this part of the expression. The parentheses have no effect and simply enclose a string literal holding a space. as such that can be combined with the next string - see my earlier answer above.


(John Doernberg) #6

You’re correct, of course: it was an unnecessary effort to create a space that is more efficiently done as you suggested.