> For the complete documentation index, see [llms.txt](https://docs.rulecube.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rulecube.com/v2.5/tutorial/using-lookup-tables.md).

# Using Lookup Tables

## Introduction

In this tutorial we will revisit Constant tables and use one of its very useful functionalities: `lookup`.&#x20;

Lookup tables allow you to implement something called **Decision tables**. This should help simplify the logic we've written in the previous tutorial where we calculated the age surcharge.

We'll be rewriting code in this tutorial, but the overall logic should not change. Our Testcase should still succeed after our changes!

### Task: Create the lookup table

1. Open the `Travel insurance calculator` from the previous tutorials.
2. Let's first delete these three constants:

   * ExtraCostPerDay18\_30
   * ExtraCostPerDay31\_50
   * ExtraCostPerDay51AndUp

   **Note**: you can do this by clicking the (...) icon of the constant in the menu and clicking Delete OR opening the constant and clicking "Delete" in the bottom right corner of the screen.
3. Create a new constant named `ExtraCostsPerDay` and set its Constant Type to **Table**

You should be somewhat familiar with Constant tables if you've followed the tutorials in order so far. Let's start filling the table with data.

1. First, click the **+ COLUMN** button to add a new column
2. Rename the `value` column to `age`
3. Rename the `value2` column to `result`&#x20;

<figure><img src="/files/y7IbnE8rdqXgIefb6bUG" alt=""><figcaption></figcaption></figure>

Keys are not as important in lookup tables, we will start with with 1 and let it automatically increment.

1. Click on the first cell (key)
2. Enter `1` in this cell
3. Press Tab to move to the `age` column
4. Enter `< 18` and press Tab
5. Leave the result cell `0` and press Enter

This should create a new row with a key that is `2`. &#x20;

Enter the following rows to complete the table:

<figure><img src="/files/PfIBuSG55P5VKRtJJmtI" alt=""><figcaption></figcaption></figure>

Alternatively, copy and paste the following CSV while you have the `ExtraCostsPerDay` constant open.

```csv
key,age,result
1,< 18,0
2,>= 18 AND <= 30,0.15
3,> 30 AND <= 50,0.1
4,> 50,0.05
```

**Note**: Make sure you are not in a text field when you paste, or the contents of the CSV will be pasted in that instead.

### How does it work?

You may have noticed that the cells under the age column are expressions and not plain values. These expressions are used when the `lookup()` method is used.&#x20;

For example you could try this in a new rule named `LookupTest`:

```javascript
ExtraCostsPerDay.lookup(20)
```

This will look up the value `20` in the table in the **first column** (the Key column is not counted) and return the value in the **last column**. This will result in `0.15`, because the second row's expression (`>= 18 AND <= 30`) satisfies for the given input of `20`.

### Task: Simplify the `AgeSurcharge` function

Next we're going to simplify the `AgeSurcharge` function. Its Function body currently should look something like this:

```javascript
if (age >= 18 && age < 31) {
    return numberOfDays * ExtraCostPerDay18_30;
}
if (age >= 31 && age < 51) {
    return numberOfDays * ExtraCostPerDay31_50;
}
if (age >= 51) {
    return numberOfDays * ExtraCostPerDay51AndUp;
}
return 0;
```

We're going to actually replace this with just a **single line of code**!

1. Delete the contents of `AgeSurcharge`'s Function body code
2. Add the following code:<br>

   ```javascript
   return numberOfDays * ExtraCostsPerDay.lookup(age);
   ```
3. Save the ruleset

If everything went well, the ruleset should work as before. If you followed the previous tutorial on adding Testcases, the Testcase we've created should still succeed, as we did not change the overall logic.

## Recap

Complex code that deals with calculations with tiered values can be rewritten using Constant tables and their `lookup()` method.&#x20;

This reduces code in your ruleset and allows for easy management of business logic.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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.rulecube.com/v2.5/tutorial/using-lookup-tables.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.
