How to search using agent a term with the $Keywords=="chemistry"

Hello,

I added $keywords attribute to my notes and assigned it as a ‘set’. This attribute is to record which subjects three students like

I created three notes: Duke, John and Prem,

For the $Keywords:

Duke: geography

John : geography; chemistry

Prem:chemistry;physics

I then created an Aget note. In the query section, I added the following action code:
$Keywords==” chemistry”. However, the agent did not pick-out the students who liked chemistry.

Where might I be going wrong? Thank you

  1. Attribute names are case sensitive. Keywords and keywords are distinct.
  2. The == operator tests for equality. Two sets are equal if each of their members are equal. You probably want $Keywords.contains(“chemistry”)

Also, string delimiters should be straight quotes ("chemistry") not typographic ‘curly’ quotes (“”). Note that the forum software assumes all quotes need to be curly (prettier text!) unless in a code block. Putting a back-tick before after code samples in posts should help or use the pre formatted text option in the post edit bar, i.e. the </> button.

So assuming $Keywords==” chemistry” was meant to be $Keywords==" chemistry", the space before ‘chemistry’ might be problem. When adding words/phrases to a Set or List, opening/trailing whit space is trimmed. If I type blue; red into $Keywords shown as a Displayed Attribute, if I click away and reselect the result is blue;red is listed. So an exact check (i.e. ==) for ’ red’ with fail, you need to use ‘red’.

But even then you are not out of the woods as $Keywords=="chemistry" will only work if a note’s $Keywords contains only one value and it is ‘chemistry’. It would not match a note where $Keywords contained physics;chemistry. It is not the order that matters but == matches the whole contents of $Keywords, i.e. all values including the semicolons in between.

Instead you want $Keywords.contains("chemistry"), or as that is a case-sensitive check, you might want $Keywords.icontains("chemistry") so any case variant of the word matches, i.e. ‘Chemistry’, ‘chemistry’, etc.

It might help with this ongoing series of problems if you post a small TBX that shows the problem, i.e. the minimum amount of configuration (effort!) needed to display the problem. this makes it significantly easier to spot edge case errors and bona fide code syntax errors.

HTH :slight_smile:

@mwra Thank you!

  1. Part of the problem was related to quotes. I have now unchecked smart quotes

  2. As you mentioned $Keywords=="chemistry" did not pick up someone who liked chemistry and physics

  3. I did try $Keywords.contains(“chemistry”) ,
    but, nothing happened i.e. Agent did not pickup any chemistry (on its own or those liked chemistry along with physics)

    Please find the test file attached to this post. Thank you in advance!

    chemistry.tbx (165.6 KB)


chemistry-v1.tbx (162.0 KB)

The error was simple to spot thanks to syntax colouring. When I opened the Get Info for the agent, the string "chemistry" wasn’t in red (as used for double-quote enclosed strings). Why, you’d used curly quotes. Fix that and you’re good to go.

@mwra Thank you!
I thought I had sorted the curly brackets/double-quote issue. Obviously not. Thank you for the help! Now, works nicely!

Great. Don’t worry unduly about how the quotes got in that form, it happens. To illustrate for later readers how action code syntax colouring helps, here is what you’d expect, i.e. after I fixed the above TBX, as shown in Get Info/agent:

The purple text indicates ‘Keywords’ is detected as an attribute (user or system) recognised in this document. The blue text indicates ‘.contains’ is recognised as a valid operator. The red text in indicates a a string of text between a pair of straight double quotes. If I had used straight single quotes (also allowable here) the same text would instead be orange. But, see what happens with curly quotes:

No colouring of the match string! That was what I spotted last night. The syntax colouring is also seen in any Inspector input box expecting action code and in notes using the built-in Action prototype. Also be aware of the built-in Code prototype doesn’t apply action code syntax colouring but does use a monospace font, turn off smart quotes, etc.—see the linked article fro the settings changes it makes. The Code prototype can be good for notes where you’d like to park some code that should be run but without the code getting damaged by quote-type-conversion. Importantly add the prototype before pasting code into the note’s $Text.

The colouring alone doesn’t solve all ills but is worth a visual check is code you think should work, doesn’t!

†. Don’t apply the Action prototype to any old note just to get syntax colouring of $Text as the prototype signals other things so the app. For general code storage (of code not being run) use the Code prototype noting that it doesn’t have syntax colouring.

‡. Note a possible confusion. The internal action code syntax colouring is not the same as using the ‘Syntx’ general highlighter found within the Hints containers.

2 Likes

@mwra Thank you! Very helpful!

1 Like

what an insightful illustration … cherry topped “inside” with a link to the respective section of what is best referred to as our golden source of truth aka atbref.

Thank you, @mwra

2 Likes

Late to the party… question for @mwra

I notice you said this:

Instead you want $Keywords.contains("chemistry"), or as that is a case-sensitive check, you might want $Keywords.contains("chemistry") so any case variant of the word matches, i.e. ‘Chemistry’, ‘chemistry’, etc.

I don’t see a difference between those two examples, but the first is case-sensitive and the second is not. What am I missing?

TIA

1 Like

My apologies, the case-insensitive variant is .icontains().

I’ll fix the earlier post.

1 Like