each
for filters> each {flags} (closure)
--keep-empty, -k
: keep empty result cellsclosure
: the closure to runinput | output |
---|---|
any | any |
list<any> | list<any> |
table | list<any> |
Multiplies elements in the list
> [123] |each {|e| 2*$e }
╭───┬───╮
│0│2│
│1│4│
│2│6│
╰───┴───╯
Produce a list of values in the record, converted to string
> {major:2, minor:1, patch:4} |values|each {|| into string }
╭───┬───╮
│0│2│
│1│1│
│2│4│
╰───┴───╯
Produce a list that has "two" for each 2 in the input
> [1232] |each {|e| if$e==2 { "two" } }
╭───┬─────╮
│0│two│
│1│two│
╰───┴─────╯
Iterate over each element, producing a list showing indexes of any 2s
> [123] |enumerate|each {|e| if$e.item==2 { $"found 2 at ($e.index)!"} }
╭───┬───────────────╮
│0│found2at1!│
╰───┴───────────────╯
Iterate over each element, keeping null results
> [123] |each--keep-empty {|e| if$e==2 { "found 2!"} }
╭───┬──────────╮
│0││
│1│found2!│
│2││
╰───┴──────────╯
Since tables are lists of records, passing a table into 'each' will iterate over each record, not necessarily each cell within it.
Avoid passing single records to this command. Since a record is a one-row structure, 'each' will only run once, behaving similar to 'do'. To iterate over a record's values, try converting it to a table with 'transpose' first.
name | type | usage |
---|---|---|
each while |
Builtin | Run a block on each row of the input list until a null is found, then create a new list with the results. |
Copyright © 2019–2023 The Nushell Project DevelopersLicensed under the MIT License.
https://www.nushell.sh/commands/docs/each.html