# Variables and Arrays

#### Variables:

You can create a variable and put a value into it using the following function:

```handlebars
{{ putValue 'variablename' value outputtotemplate? }}
```

example:

```handlebars
{{ putValue 'id' (Request.Body 'jsonpath' '$.id') true }}
```

(The *outputtotemplate* boolean will determine if the value is written out to the template as well as stored in memory, or just stored in memory.)

You can read a variable and use it in another function, or simply render it into the template using the following function:

```handlebars
{{ getValue 'variablename' }}
```

example: simply rendering out:

```handlebars
{{ getValue 'price' }}
```

example: using within another function:

```handlebars
{{ add (getValue 'price') this.tax '0.00' }}
```

#### Arrays:

You can append items to an array using the following function: (this is useful within a loop when you need to sum a list of numbers at the end)

```handlebars
{{ addToArray 'arrayname' value outputtotemplate?}}
```

example:

```handlebars
{{ addToArray 'totalweight' (multiply (this.quantity) (csv 'products' 'upc' 'this.upc' 'weight') '') false}}
```

(The *outputtotemplate* boolean will determine if the value is written out to the template as well as appended to the array, or just appended to the array.)

You would generally not render an array straight into the template - they would typically be used by another function or block statement that takes arrays, using the following function:

```handlebars
{{ getArray <arrayname> }}
```

example: passing into another function:

```handlebars
{{ sum (getArray 'totalweight') '0.00' }}
```

example: adding a new column to an in memory csv data store:

```handlebars
{{ addToArray 'newPet' '2000' false }} 
{{ addToArray 'newPet' 'dogs' false }} 
{{ addToArray 'newPet' 'Violet' false }} 
{{ addToArray 'newPet' 'sold' false }}

{{ csvAddRow 'pets' (getArray 'newPet') }}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cloud.hoverfly.io/create-simulations/using-templating-in-simulations/variables-and-arrays.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
