Howdy, Stranger!

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

Using a variable for the index of an array.

So let's say I do this

(set: $example to (a:&quot;Example A&quot;,&quot;Example B&quot;,&quot;Example C&quot;)<br />(set: $x to (either:0,1,2))

If I then (print: $example[$x]), Twine will output one of the three strings in the array. So far so good, but how do I CHANGE the string at $example[$x]?  I thought it would have been something like (set: $example[$x] to &quot;Example D&quot;), but that gives me the error "I can't give undefined a new value."

If I was just using a number I could say $example&#039;s 1st instead of $example[0], but how do I do that with a variable? Is it possible?

Comments

  • I'm trying to figure this out myself ... I think you use splice!
  • The Harlowe documentation says to access an item in an array you use the following syntax:
    (set: $list to (a: "one", "two", "three"))
    
    2nd element in List is: (print: $list's 2nd)
    

    If you read the 1.1.0 changes: New Features section of the Harlowe project overview you will see that the above syntax has been extended to also allow the use of $variables in the place of 2nd
    Added computed property indexing syntax. Properties on collections can now be accessed via a variant of the possessive syntax: $a's (expression).

    Using this syntax, you can supply numbers as 1-indexed indices to arrays and strings. So, "Red"'s $i, where $i is 1, would be the same as "Red"'s 1st. Note, however, that if $i was the string "1st", it would also work too - but not if it was just the string "1"
    The following example is based on both of the above.
    (set: $list to (a: "one", "two", "three"))
    (set: $x to 2)
    
    1st element in List is: (print: $list's 1st)
    2nd element in List is: (print: $list's $x)
    
    (set: $list's $x to "aaa")
    
    1st element in List is still: (print: $list's 1st)
    2nd element in List is now: (print: $list's $x)
    
Sign In or Register to comment.