Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Twine 2, Harlowe] How to check a string for containing multiple keywords?

I would like to have the user input a phrase and have a response printed if any one of a number of certain keywords is detected in the string that the user made. I'm able to implement the "contain" operator with an (if:) or (unless:) statement just fine for only one word. However, the moment I try to add more strings I get back an error:

I can only use 'or' to join booleans, not the string "[any string i put]".►
If one of these values is a number, you may want to write a check that it 'is not 0'. Also, if one is a string, you may want to write a check that it 'is not "" '.
These strings are never 0 or empty. I write them in the format of
(if: "word" or "phrase" is in $greeting)
or
(if: $input contains "word" or "phrase")
And it always spits back that "or" error even though I am using "or".

Can anyone tell me how I might implement a check for multiple strings? Thanks.

Comments

  • If you look at the (if:) macro's documentation you will see that it takes a Boolean expression as a parameter.

    eg. "phrase" is in $greeting and $input contains "word" are both Boolean expressions.

    If you read the OR operator's Purpose within the Boolean data section you will you see that if either side equals true then the overall expression will equal true.

    This infers that an expression can be made up of two (or more) sub-expressions joined by an OR (or AND) operator.
    (if: "word" is in $greeting or "phrase" is in $greeting)
    
    or
    
    (if: $input contains "word" or $input contains "phrase")
    

    p.s. Advance String searches using Regular Expressions and the String object's search() method.

    It would take too long to explain how to use Regex in any detail but the above example could of also be written like so:
    (if: $greeting.search('word|phrase') is not -1)
    
    or
    
    (if: $input.search('word|phrase') is not -1)
    
Sign In or Register to comment.