Dynamic Sub-list Referencing

## Introduction

This page talks about how to dynamically reference a hierarchical list. This is a very useful for deep hierarchies that reference or depend on the output of the other.

## Examples

### The Problem

Have a look at this simple generator:

```
output
  My gender is [g = gender.selectOne] and my name is [names.g.selectOne].

gender = {female|male|non-binary^0.1}

names
  female
     Anita
     Jessica
  male
     Kalid
     Bob
  non-binary
     Airlie
     Riley

```

But there's something wrong with this generator - I've marked it red. What I wanted to do here is use the correct sublist of `names` depending on what is stored in the `g` variable. As it currently stands, this generator would throw us an error because there's no "g" sublist in the `names` names list; `names.g` doesn't exist.

So how do we do this? We just want to get sublist of `names` that has the same name as the value stored in the `g` variable. Before I present the solution, I want to re-iterate something that you learned in the basic tutorial: Square brackets have a completely different meaning when they're used *inside* square brackets. When you're inside square brackets you can refer to variables and lists directly by their names, so:

* This is **wrong**: [a = [animal].selectOne]
* This is right: [a = animal.selectOne]

Makes sense? We don't need to use square brackets to refer to lists and variables when we're already inside square brackets.

### The Solution

The reason I wanted to re-iterate that is because you're about to learn one of the *actual uses* of square brackets *inside* square brackets. Without further ado, here's the solution to our previous problem:

```
output
  My gender is [g = gender.selectOne] and my name is [names[g].selectOne].

```

So we've replaced `.g` with `[g]`. This tells the Perchance engine to use the *value* stored in `g` as the name of the sublist, rather than using the literal character "g". So if the value in `g` was "other", then writing `name[g]` would be the same as writing `name.other`.

Remember, these square brackets are part of a special notation that is completely different to normal usage of square brackets.

### Chaining Sub-list Referencing

Often we need to "chain" sub-property names together to go down a hierarchy: `world.continent.country.state.city` - and we can do the same thing with our dynamic notation: `world[a][b][c][d]`, and we can mix and match normal and dynamic notations: `world[a].country[b][c].town`.

Sometimes you may find that you need to combine variables to create your property names. You can do that like so: `thing[a+b]`. That will join `a` and `b` together, and then get the sub-list of `thing` with that name (if it exists). You can also combine variables with literal strings like this: `thing[a+"blah"]`. So if the value of `a` was "foo", then that would be the same as writing `thing["fooblah"]`, which would be the same as writing `thing.fooblah`.

Here's a simple generator that selects a random gender and then uses that to select an appropriate appearance for a character. And here's another that matches genders with the correct pronouns. And here's a very simple example.