CH03-L01 · foundation

Lists and indexes

Select an item from a list and see what an invalid index does.

General CELRuns hereDocumented
What will I learn?

You know that the first item has index 0 and that an out-of-range access is an error.

Where will I try it?

Directly below, in the browser playground.

What do I do?

Run both modes, compare where their values come from, then complete the task.

Try CEL · browser pilot

Try both ways to supply values

Define local values inside one CEL expression, or provide variables as JSON input. Everything runs in your browser.

CEL pilot subset
One self-contained CEL expressioncel.bind() gives a local name to a value and returns the result of the final expression.

cel.bind()cel.bind() is an optional official extension. It is not part of the core CEL language and may not be enabled by every host application.

Tip: press Ctrl/⌘ + Enter to run.
Result

Press Run CEL to evaluate the active example.

Pilot scope: literals, explicit conversions, local cel.bind() variables, JSON variables, lists, maps, presence, collection macros, core string checks, time values, arithmetic, comparisons, logic, and the conditional operator.

What you will do

You will run the prepared expression in the playground above, change one input at a time, and explain the result. Start in CEL only, then repeat the same rule in CEL + JSON.

How it works

A list is an ordered collection. CEL indexes lists from zero: index 0 is the first item, 1 is the second, and so on. The index must be an int, and reading past the end is an error rather than a null result.

Read the prepared example

The CEL + JSON rule is:

items[1]

Its input is:

{
  "items": ["draft", "review", "approved"]
}

The CEL only tab contains the values and the rule in one expression. The enabled cel.bind() extension keeps each name local to its final argument.

Evaluation step by step

  1. items contains three strings in a fixed order.
  2. Index 1 selects the second position.
  3. The result is "review".

Your task

  1. Run the example.
  2. Change the index to 0, then to 2.
  3. Try index 3, read the range error, and restore 1.

Expected observation: Indexes 0, 1, and 2 return the three items; index 3 is outside the list.

Common mistake

Do not confuse the number of items with the last valid index. A list of size 3 ends at index 2.

Show the explanation

4.

Knowledge check

What is the last valid index of a list whose size is 5?

Key takeaway

List order is stable, indexing starts at zero, and invalid access is an explicit error.

Sources

  • CEL-DEV — official CEL overview.
  • CEL-LANG — official CEL language definition.
  • CEL-GO-BIND — official documentation for the optional cel.bind() extension used by the self-contained tab.