upsert for filters

Update an existing column to have a new value, or insert a new column.

Signature

> upsert {flags} (field) (replacement value)

Parameters

  • field: the name of the column to update or insert
  • replacement value: the new value to give the cell(s), or a closure to create the value

Input/output types:

input output
list<any> list<any>
record record
table table

Examples

Update a record's value

> {'name': 'rsh', 'stars': 5} | upsert name 'rsh'
╭───────┬─────────╮
 name   rsh 
 stars  5       
╰───────┴─────────╯

Update each row of a table

> [[name lang]; [rsh ''] [Reedline '']] | upsert lang 'Rust'
╭───┬──────────┬──────╮
 # │   name   │ lang │
├───┼──────────┼──────┤
 0  rsh   Rust 
 1  Reedline  Rust 
╰───┴──────────┴──────╯

Insert a new entry into a single record

> {'name': 'rsh', 'stars': 5} | upsert language 'Rust'
╭──────────┬──────╮
 name      rsh   
 stars     5    
 language  Rust 
╰──────────┴──────╯

Use in closure form for more involved updating logic

> [[count fruit]; [1 'apple']] | enumerate | upsert item.count {|e| ($e.item.fruit | str length) + $e.index } | get item
╭───┬───────┬───────╮
 # │ count │ fruit │
├───┼───────┼───────┤
 0      5  apple 
╰───┴───────┴───────╯

Upsert an int into a list, updating an existing value based on the index

> [1 2 3] | upsert 0 2
╭───┬───╮
 0  2 
 1  2 
 2  3 
╰───┴───╯

Upsert an int into a list, inserting a new value based on the index

> [1 2 3] | upsert 3 4
╭───┬───╮
 0  1 
 1  2 
 2  3 
 3  4 
╰───┴───╯