Howdy, Stranger!

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

string compare versus something in an array? (Harlowe)

I think I am (once again) missing something very basic here - perhaps array content having to be de-referenced or something?
(set: $_errorCodes to (array: "NA","NA","NA"))
(print: $_errorCodes's 1st)
(if: $_errorCode's 1st is "NA")[1st was "NA"]
(elseif: $_errorCode's 1st is not "NA")[1st was NOT "NA"]
(if: $_errorCode's 1st contains "NA")[1st contains NA]

comes out as

1st was NOT "NA"

Is there a way (in Harlowe) of figuring out if a string in a given array == another string? I've tried a few other variations of above (brackets or parenthesis around $_errorCode's 1st, not putting quotation marks around "NA", etc. no luck), the Harlowe documentation only talks about 'is', 'is not' and 'contains' (which, as above, does not work - contains having the added problem of not being a hard compare, so if a parameter was something like "Nature" it would come out as a match) I can't seem to find anything explicitly in the forums about this (though I'm sure someone's code must be doing it), and I don't want to necessarily check 'if a string is *anywhere* in the array' so much as at a specific index?


  • Two issues with your example code:

    a. You left of the s character at the end of the $_errorCodes variable name in the (if:) and (elseif:) macros.

    b. The contains keyword is meant to be used on an array, not an array element.
    (if: $_errorCodes contains "NA")[array contains NA]

    These is also a possible future issue with your $_errorCodes variable name, the developer of Harlowe is currently planing on using the underscore character _ to indicate local variable scope. I don't know if this will effect you or not, it depends on the regex used and the order they are processed.
  • Oh geez... I can't believe I was doing that, beat my head against for two days trying different things, all while cutting and pasting the miss-spelled bits. Thank you!

    I feel incredibly silly now - also thanks for letting me know about the _ ! I'll come up with something different, I was actually going to use it as a way of indicating global variables in my code so I wouldn't accidentally trip over them. Yay for letting me know about this before I got too far into the new revision :D
Sign In or Register to comment.