Ink & arrays workaround

Seeing that Ink doesn’t support arrays (nor “for” loops), I am looking for workarounds.

I usually use arrays to give random replies, for example "You pick up a random book. It is … [look up a random entry in the relevant array]. Inform does that pretty well with its tables (which are not exactly 1-D arrays—more like arrays of dictionaries/objects of key:value pairs).

Anyway, Ink doesn’t have any of that, so this is the best I could come up with, using its LIST feature and switch statements. If anyone has other ideas of workarounds, please chip in.

The following example associates a LIST of first names with their full names and ages.

LIST firstNames =   Steve,

VAR current = Steve // we start at the first member of firstNames

The members of Iron Maiden currently are: <>

- (for) // the beginning of a devised "for" loop

    ~ whoIs(current) // calling the function that prints full names
    <> (<>
    ~ howOld(current) // calling the function that prints the age

    { // if we haven't exhausted list, loop
        - LIST_VALUE(current) < LIST_COUNT(LIST_ALL(firstNames)):
            ~ current++
            <>, <> // some glue needed to print all in one line.
                { current == Nicko: and <>} // check for final name's "and"
            -> for //return to beginning of loop.
    <>. // final period.

=== function whoIs(theirFirstName) ===

{   theirFirstName:
        - Dave:     Dave Murray
        - Steve:    Steve Harris
        - Bruce:    Bruce Dickinson
        - Andrian:  Andrian Smith
        - Janick:   Janick Gers
        - Nicko:    Nicko McBrain

=== function howOld(theirFirstName) ===

{   theirFirstName:
        - Dave:     64
        - Steve:    65
        - Bruce:    63
        - Andrian:  64
        - Janick:   64
        - Nicko:    69

This produces the desired:

The members of Iron Maiden currently are: Steve Harris (65), Dave Murray (64), Bruce Dickinson (63), Andrian Smith (64), Janick Gers (64), and Nicko McBrain (69).

I repeat, if anyone has other ideas of workarounds, please chip in.


With key-value pair, you can just use 1…N as key to simulate arrays.
This will also work for 2D arrays.

I have simulated such associative arrays by using string

1:apple 2:banana 3:cherry 4: durian

Then a simple string search " N:" will give me the index of desired key-value pair.

1 Like

It feels to me like you’re asking a couple different questions, maybe? Like for random replies, you might just use a random shuffle of book names?

And…I could be wrong about this, but can’t you use lists as arrays? I thought they associated each item with a number and you could lookup both ways: LIST_VALUE(Bruce) gives 3 while firstNames(3) gives Bruce? Although I guess list names have to be valid identifiers, so they can’t be numbers or multiple words.

I don’t know if the nicer list printing snippet makes sense here (seems like you’d want another function to look up the full name and age, but it does show an alternate way to do the recursion.

Anyway. You probably know all that already, but just in case it sparks something for you…

1 Like

Hey thanks for the reply! Could you elaborate with an example of a few lines of code? I didn’t understand how you use the 1:apple 2:banana 3:cherry 4:durian line. And how do you do a string search? And how does it work for 2D arrays, too? :thinking:

Yes, of course, that’s enough if all I need is a random string output at that moment, but I sometimes want the program to remember the outcome. For example, the player can pick up a random book and read the title immediately, but then they can get the option to read the blurb on the back cover, too. So, there must be a way to store which book we picked in random, so we can use it later on, associating it with other specific replies.

That’s exactly my problem, that I can’t simply store strings in a list. I can use single-word entries as they are, as in LIST inventory = sword, cheese, mousetrap, but not for items like bastard sword, fermented curd, King Louis XIV. Hence the elaborate solution for associating the first names with the full names via a function.

Thanks for the support, though! :sunglasses:

Unfortunately, Ink doesn’t do string operations internally. You need to do it externally because the string is built different as compared to traditional programming language.

So, I’m afraid I can’t give you code samples in Ink. Sorry.