Home Explore Blog CI



nushell

1st chunk of `commands/docs/each_while.md`
e90f16731efe9a2f8de2fd65b15c467642f9eb29ed5b84ba00000001000006a8
---
title: each while
categories: |
  filters
version: 0.104.0
filters: |
  Run a closure on each row of the input list until a null is found, then create a new list with the results.
usage: |
  Run a closure on each row of the input list until a null is found, then create a new list with the results.
editLink: false
contributors: false
---
<!-- This file is automatically generated. Please edit the command in https://github.com/nushell/nushell instead. -->

# `each while` for [filters](/commands/categories/filters.md)

<div class='command-title'>Run a closure on each row of the input list until a null is found, then create a new list with the results.</div>

## Signature

```> each while {flags} (closure)```

## Parameters

 -  `closure`: The closure to run.


## Input/output types:

| input     | output    |
| --------- | --------- |
| list\<any\> | list\<any\> |
## Examples

Produces a list of each element before the 3, doubled
```nu
> [1 2 3 2 1] | each while {|e| if $e < 3 { $e * 2 } }
╭───┬───╮
│ 0 │ 2 │
│ 1 │ 4 │
╰───┴───╯

```

Output elements until reaching 'stop'
```nu
> [1 2 stop 3 4] | each while {|e| if $e != 'stop' { $"Output: ($e)" } }
╭───┬───────────╮
│ 0 │ Output: 1 │
│ 1 │ Output: 2 │
╰───┴───────────╯

```

Iterate over each element, printing the matching value and its index
```nu
> [1 2 3] | enumerate | each while {|e| if $e.item < 2 { $"value ($e.item) at ($e.index)!"} }
╭───┬───────────────╮
│ 0 │ value 1 at 0! │
╰───┴───────────────╯

```

Title: each while: Filter Command in Nushell
Summary
The `each while` command in Nushell iterates over a list, applying a closure to each element until a null value is encountered. It then returns a new list containing the results of the closure. The documentation provides examples of how to use `each while` with conditions and enumeration to process lists based on specific criteria.