Howdy, Stranger!

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

Modifying state of array element in Harlowe 2.0.11

So I've declared
(set: $foo to (a: false, false, false))

For some reason, I can
(set: $foo's 1st to true)
but I can't
(set: $foo[0] to true)

The latter keeps saying unexpected identifier. I'm trying to do something kind of complicated but in order to isolate the problem I wrote this simple code:
(set: $foo's 1st to true)
(if: $foo's 1st is true)[bar]
That works. This doesn't??
(set: $foo[0] to true)
(if: $foo[0] is true)[bar]

I want to use a variable in the $foo array so I don't have to repeat code. Am I doing something wrong...?

Comments

  • I mean Harlowe 1.2, Twine 2.0.11, my bad.
  • Harlowe does not support Javascript's syntax for accessing an array element, but if you reed the Array documentation you will see that you can use the $variable's (index) syntax instead.
    (set: $array to (array: "one", "two", "three"))
    (set: $index to 1)
    
    1st element (number): (print: $array's (1))
    
    (set: $array's ($index) to "not one")
    1st element (variable): (print: $array's ($index))
    
    note: Harlowe's arrays are one based, using zero will return the last element of the array.
  • Ahhhh thank you! I don't think the parentheses are needed around the index but I got it to work. Thanks!
  • edited December 2016
    I don't think the parentheses are needed around the index
    I use the parentheses because the documentation states you should, because ($index) is an expression. If the Harlowe markup parser currently supports not using the parentheses then that's fine unless that is a bug, in which case not using parentheses could stop working in a later version.

    Experience has taught me that just because something works does not mean it's how you should do it. lol
  • i tried using the parentheses but it returned nothing :o so I took them out
  • i tried using the parentheses but it returned nothing :o so I took them out
    That's strand because I test my example code before posting it.
  • i must've typed it wrong. sorry
Sign In or Register to comment.