Execution Order

## Introduction

This page shows the importance of execution order in Perchance that can avoid generator errors and unexpected outputs.

## Examples

### Problem #1

#### Problem

Can you see something wrong with this example?

```
name = {import:common-first-name}

output
  [n] said "My name is [n = name.selectOne]" and then {ran away|just stood there}.

```

The Perchance engine "reads" the code in a left-to-right order. So, the problem is that Perchance reads `[n]` before we've created the `n` variable. As you can see in this example generator, Perchance gives us an error telling us that `[n]` returned "undefined", meaning that there was no value stored in the `n` variable when we tried to access it to generate the start of our sentence.

#### Solution

Here's a fixed version of the `output` list:

```
output
  [n = name.selectOne] said "My name is [n]" and then {ran away|just stood there}.

```

### Problem #2

#### Problem

Let's look at a slightly more complicated version where we've made the same mistake as the original example:

```
name = {import:common-first-name}

output
  [n] said "[say]" and then {ran away|just stood there}.

say
  My name is [n = name.selectOne].

```

It's really the same example, except that we've pulled the "My name is..." out into its own list - maybe because in the future we want to have multiple different `say` possibilities. But notice that we've made the same mistake as before: We're written `[n]` right at the start of the `output` line - before we've defined what `n` actually is.

#### Solution

So, here's a fixed version:

```
name = {import:common-first-name}

output
  [n = name.selectOne] said "[say]" and then {ran away|just stood there}.

say
  My name is [n].

```

We put a name in the `n` variable before it gets used by the `[say]` block. The code now works just like we want it to.

This is a good opportunity to learn about an important aspect of the Perchance engine: If you create a variable in one list, then you can access it from any other list, so long as that other list is "read" by the Perchance engine after you've created the variable.

### Good Example

Let's look at an example:

```
name = {import:common-first-name}
noun = {import:concrete-noun}

output
  [n = name.selectOne] said "[say]" They then pulled several [things] out of their bag.

say
  My name is [n]. I like [things = noun.selectOne.pluralForm].

```

Notice that we've created a variable in the `say` list, but then used it in the `output` list. That's perfectly fine, since we've written `[thing]` after creating the `thing` variable. Remember, the Perchance engine reads from left-to-right, so we know that it has already read the `[say]` part of the `output` line (which is where `thing` is created) before it gets to the `[thing]` part of the `output` line.

If that sounds complicated, just read your code like you would normal text, and mentally substitute square blocks with random items from the relevant list. If you see a variable used before it has actually been created, then you know you've got a problem.

### HTML Execution Order

One final note: The text in the HTML panel is also "read" by the Perchance engine from left-to-right, top-to-bottom - just like a book (an English book, at least). So let's say your HTML editor panel contained this code:

```
<h1>[title]</h1>
<p>[output1]</p>
<p>[output2]</p>
<button onclick="update()">[buttonText]</button>

```
In this case the Perchance engine would first pull a random item from the `title` list, and then "read" it, and then do the same for `output1`, then for `output2`, and then for `buttonText`. So if you define a variable in `output2`, you won't be able to use it in the items of your `output1` list. In other words, the same ordering rules apply for the HTML panel too.