Howdy, Stranger!

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

Array Indexing at a Variable's Value? Hello!

  Hey there!

  I hate to concatenate my debut greetings to the forum to a plea of help, but maybe I can accomplish both in one post.

  Hello. I look forward to getting to know members old and new, to help and probably to annoy, to learn and to, most of all, enjoy. Pleased to meet you all.

  Now to the less important stuff: the new syntax for variable indexing in Twine 2.0 is noted at http://twine2.neocities.org/ to be more naturally linguistic in syntax. You say, for array variable x, $x's 1st to return x's first item in the index. Cool! I love the easiness involved in remembering that and writing it out. But what if someone wanted to return x's "$nth?" I discovered that one can use the suffixes st, nd, rd, and th (as in "firST, secoND, thiRD and fourTH" etc.) interchangeably, i.e. $x's 2st, which makes it easier to use variables. Still, accessing an array's customized index is not a talent I have. I have been testing guesses, but no luck so far. Any help? If I am totally missing something, I apologize for the recklessness.

  Nice to meet you!

  Cecil

Comments

  • Based on the documentation you could use the (subarray:) macro.

    If you had an array with three elements and you wanted to print the 2nd element or assign it to a variable you could do the following:

    (set: $array to (a: "One", "Two", "Three"))
    (set: $index to 2)

    (print: (subarray: $array, $index, $index))

    (set: $var to (subarray: $array, $index, $index))
    $var
  • I'm still figuring out how best to implement computed index access.

    I've been thinking something like " $x's ($a) " or "$x's ($a)'s 1st" might do it, but I feel like this special-case usage of brackets is a bit unworkable, or at the very least immemorable. I certainly won't resort to a special macro for this, though.
Sign In or Register to comment.