# pop

Removes and returns the last element from an array. The original array is modified (mutated) and the removed element can be stored in a variable.

```liquid
{% json items %}[1, 2, 3, 4, 5]{% endjson %}
{% pop items as last_item %}

{% log last_item %}
{% comment %} Output: 5 {% endcomment %}

{% log items %}
{% comment %} Output: [1, 2, 3, 4] {% endcomment %}
```

#### Syntax

```liquid
{% pop array_name as variable_name %}
```

#### Parameters

| Parameter          | Required | Description                               |
| ------------------ | -------- | ----------------------------------------- |
| `array_name`       | Yes      | The array to remove the last element from |
| `as variable_name` | No       | Variable to store the removed element     |

#### Behavior

* Removes the **last** element from the array
* Modifies the original array (mutation)
* Returns `null` if the array is empty
* Supports nested array access with dot notation

#### Examples

**Basic pop operation:**

```liquid
{% json queue %}["task1", "task2", "task3"]{% endjson %}
{% pop queue as task %}

{% log "Processing: " | append: task %}
{% comment %} Output: Processing: task3 {% endcomment %}
```

**Pop without storing the value:**

```liquid
{% json numbers %}[1, 2, 3, 4, 5]{% endjson %}
{% pop numbers %}

{% log numbers %}
{% comment %} Output: [1, 2, 3, 4] {% endcomment %}
```

**Process array in reverse (LIFO):**

```liquid
{% json stack %}["first", "second", "third"]{% endjson %}

{% for i in (1..3) %}
  {% pop stack as item %}
  {% if item %}
    {% log "Popped: " | append: item %}
  {% endif %}
{% endfor %}

{% comment %}
Output:
Popped: third
Popped: second
Popped: first
{% endcomment %}
```

**Use with push for stack operations:**

```liquid
{% json undo_stack %}[]{% endjson %}

{% comment %} Add actions to stack {% endcomment %}
{% push undo_stack, "action1" %}
{% push undo_stack, "action2" %}
{% push undo_stack, "action3" %}

{% log "Stack: " %}
{% log undo_stack %}
{% comment %} Output: ["action1", "action2", "action3"] {% endcomment %}

{% comment %} Undo last action {% endcomment %}
{% pop undo_stack as last_action %}
{% log "Undoing: " | append: last_action %}
{% comment %} Output: Undoing: action3 {% endcomment %}
```

**Pop from nested array:**

```liquid
{% json data %}
  {
    "pending": ["order1", "order2", "order3"],
    "completed": []
  }
{% endjson %}

{% pop data.pending as order %}
{% log "Processing order: " | append: order %}
{% comment %} Output: Processing order: order3 {% endcomment %}
```

**Batch processing with pop:**

```liquid
{% json items_to_process %}
  [
    {"id": 1, "name": "Item A"},
    {"id": 2, "name": "Item B"},
    {"id": 3, "name": "Item C"}
  ]
{% endjson %}

{% assign processed_count = 0 %}

{% for i in (1..100) %}
  {% if items_to_process.size == 0 %}
    {% break %}
  {% endif %}

  {% pop items_to_process as item %}
  {% log "Processing: " | append: item.name %}

  {% comment %} Process item... {% endcomment %}

  {% assign processed_count = processed_count | plus: 1 %}
{% endfor %}

{% log "Total processed: " | append: processed_count %}
```

**Combine with conditionals:**

```liquid
{% json errors %}["Error 1", "Error 2"]{% endjson %}

{% if errors.size > 0 %}
  {% pop errors as latest_error %}
  {% log "Latest error: " | append: latest_error %}
{% else %}
  {% log "No errors" %}
{% endif %}
```

#### Related Tags

| Tag    | Description                            |
| ------ | -------------------------------------- |
| `push` | Adds an element to the end of an array |

#### Notes

* The array must exist and be a valid array, otherwise an error is thrown
* Pop modifies the original array - use with caution if you need to preserve the original
* Useful for implementing stack (LIFO - Last In, First Out) data structures
* When the array is empty, the popped value will be `null`/`undefined`
* Supports dot notation for nested arrays (e.g., `data.items`)
