Use .contains() to find pattern in $Name

All of a sudden my test generator stopped working after mac os upgrade and I was left with a heap of printed tests. I decided to make a test generating machine in TB.
I created several new attributes QuestionAnswer (Q/A) to differentiate between the two.
My printed test Answers start with [ ]. I would like to mark all the notes starting with brackets as $QuestionAnswer = A. I wrote a stamp:

If($Name.contains("[")){$QuestionAnswer = A ;}

I also tried:

If($Name.containsAnyOf("[;]")){$QuestionAnswer = A ;}

But nothing worked. Where I am missing something? Thank you.

I think .contains uses regular expressions, so you may need to escape the [ (because [ has a defined meaning in regular expressions).

i.e. use if ($Name.contains("\[")) ... etc

Note the \ before the [.

In general, if an expression like .contains, .replace etc doesn’t pick up a pattern containing punctuation – such as ^, $, [, ], (, ) ( and there are probably more) – it’s worth trying to escape them with \ to see if that works.

Does that help?

As @brookter rightly points out, you are searching for regular expression (‘regex’) special characters. “But,” you might say, “I didn’t know”. Indeed, but nor does action code parser! Anything you type as an input in .contains() and .icontains() should be treated as if it were regex code (which is easily looked up online if unclear.

So, why did code that used to work not work now? Tinderbox is over twenty years old; old by software standards. What is possible now, wasn’t possible then so the parsable outcomes of action code then were narrower than now. In 2001, when Tinderbox debuted, action code like this was normal:

Color - blue

But since v4.5 in mid-2008—13+ years ago—this has been deprecated in favour of more explicit writing:

$Color = "blue";

The whitespace in the latter doesn’t matter: everything else, including case-sensitivity, does matter.

So if your existing, old, old code (looking at your opening examples) fails, your first action should be to update to not use legacy syntax. That’s not a slur on you code, but time moves on. If unsure how to update, just post the old code here and forum members can help you update it. :slight_smile:

For instance:

If($Name.contains("[")){$QuestionAnswer = A ;}

Would now be written:

if($Name.contains("\[")){$QuestionAnswer = "A";}

Note:

  • if is lower case and case sensitive
  • the [ in the search pattern is a regex character so must be escaped.
  • literal strings like 'A should be enclosed in quotes (matched single or double, double being the more normal choice).

If the test is that the $Name begins with a [ character, then the correct test is:

if($Name.contains("^\[")){$QuestionAnswer = "A";}

The extra regex ^ ensures that ‘[’ is the first character of $Name.

Here’s a test: Code-update.tbx (75.9 KB)

David, thank you! It worked fine. I’ll always keep in mind regular expressions&

1 Like

Mark, thank you very much for your help. Some things I still don’t quite get. How should I assign properly boolean attributes like true/false?
If($Name.contains(“x”)){$CorrectAnswer = “true”;} doesn’t work.

Boolean attributes are always written all-lower-case and never in quotes (see more). So:

If($Name.contains(“x”)){$CorrectAnswer = “true”;}

needs 3 corrections (some may be due to forum autoformatting, if so apologies):

  1. Action codes are case-sensitive. So If becomes if.
  2. Quoting strings needs straight quotes and not typographic (‘curly’) quote. So "X" not “X”.
  3. Boolean values are not quoted: so true not “true”.

Together:

if($Name.contains("x")){$CorrectAnswer = true;}

Does that help?

1 Like