Executing & Hiding the Result

## Introduction

This page explains how to execute code but hiding the result.

```
dice = {import:dice-plugin}

output
  [a=dice("1d6"), b=dice("1d6"), c=dice("1d6"), ""]If a+b+c is [a+b+c], and a is [a], and b is [b], then what is c? (Answer: [c])

```

Here we're importing the dice plugin, using it to initialize some variables (but hiding them by adding `,""` at the end of the initialisation block), and then using those variables in the output text.

But It's a little bit messy having all our variable initalizations at the start of our output. What if we pull them out into their own list and reference them like this?

```
init
  [a=dice("1d6"), b=dice("1d6"), c=dice("1d6")]

output
  [init, ""]If a+b+c is [a+b+c], and a is [a], and b is [b], then what is c? (Answer: [c])

```

Will that work? It *looks* like it will work, but it actually won't. Let's think about what's happening here. When we write `[init, ""]` we're not actually "doing" anything with the `init` list. We're just "mentioning" it. We're not "executing" it - that is, we're not selecting an item from it and evaluating the square and curly blocks to produce some text. *Nothing* at all happens if you just "mention" a list name like that.

But you're probably thinking: Wait, but when we write `[animal]` to get a random item from our (hypothetical) `animal` list, then we're just "mentioning" the animal list, right? And that type of mention *does* "execute" the list (i.e. it selects an item and converts it to plain text by evaluating square and curly blocks). Yes! That is correct.

But it only does this because the last thing in that `[animal]` square block (and of course the *only* thing in it) is a list, and so that's what is actually going to get output, and so it needs to be converted into some text, so the Perchance engine "realizes" this and automatically executes the `.evaluateItem` command on the `animal` list, which triggers `selectOne` and then evaluates all the square and curly blocks in the resulting item.

Okay, that was a bit of a mouthful. In summary: **If the final thing in a square block is a list, then Perchance does some ✨magical✨ stuff behind the scenes to convert that list into plain text (which of course involves evaluation of square/curly blocks).**

### The Solutions

#### `init.evaluateItem` and `""`

If we want a list to be executed that's **not** the final thing in a square block, then we need to handle that ourself. Here's one way to fix the above example:

```
init
  [a=dice("1d6"), b=dice("1d6"), c=dice("1d6")]

output
  [init.evaluateItem, ""]If a+b+c is [a+b+c], and a is [a], and b is [b], then what is c? (Answer: [c])

```

As you can see, we can use the `.evaluateItem` command on any lists that we want to "execute" behind the scenes. Here's an example generator based on that code.

The important thing to remember here is that if a list isn't the last thing in a square block, it won't get "executed" automatically. "Mentioning" a list name doesn't do anything. Use `.evaluateItem` to manually "execute" the list as needed.

#### `""` on the `init`

Here's another way to solve it:

```
init
  [a=dice("1d6"), b=dice("1d6"), c=dice("1d6"), ""]

output
  [init]If a+b+c is [a+b+c], and a is [a], and b is [b], then what is c? (Answer: [c])

```

Since the block at the start of our `output` now has the `init` list as the *final* item, it will be magically executed "behind the scenes".

#### `init` and `=`

There is another way to solve this that you probably didn't expect. It involves a fairly subtle feature of Perchance that is worth understanding if you're building complex generators.

```
init = [a=dice("1d6"), b=dice("1d6"), c=dice("1d6")]

output
  [init, ""]If a+b+c is [a+b+c], and a is [a], and b is [b], then what is c? (Answer: [c])

```

You're probably looking at that and saying "But isn't that just the same as the first example? Using the equals sign like that is just a short-hand for a single-item list, right?" Well, most of the time, yes, but if the "single item" of this "list" is a ***single* square block** (like it is in this case), then mentioning the name of this "list" will trigger an execution of that square block.

So, really, if you use the "equals" single-item short-hand with an item that is just one square block, then you're creating what might be called a "random variable" or "dynamic variable" rather than a "list". So, whenever you "mention" this variable, a new dynamic value is generated. That's why the above example works.