split(): turn a CSV header and row into a record
Hard
A contacts file starts with a header line naming its columns, then one line per person. Given the header and one data line, return the person as a record whose field names come from the header.
"name,email" and "Ann,ann@example.com" give a!map(name: "Ann", email: "ann@example.com").
Rules
- spaces around names and values are ignored
- a blank column in the header, like the middle of
"name,,city", is skipped, together with whatever value sits in that column - a data line that is too short gives
""for the missing values
Watch out
- Each value belongs to the column at the same position.
- Dropping a blank header on its own would shift every later value into the wrong field, so pick the positions to keep and read both lists at those positions.
Inputs
ri!header: Textri!row: Text
ri! Rule Inputs
ri!headerText"name,email,city"
ri!rowText"Ann,ann@example.com,Pune"
Example 1: A clean line
Input: ri!header
"name,email,city"
Input: ri!row
"Ann,ann@example.com,Pune"
Output:
a!map(name: "Ann", email: "ann@example.com", city: "Pune")
Example 2: Spaces everywhere
Input: ri!header
" name , email "
Input: ri!row
" Bo , bo@example.com "
Output:
a!map(name: "Bo", email: "bo@example.com")
Example 3: A blank column is skipped, not shifted
Input: ri!header
"name,,city"
Input: ri!row
"Cy,IGNORED,Delhi"
Output:
a!map(name: "Cy", city: "Delhi")
Example 4: A short data line
Input: ri!header
"name,email,city"
Input: ri!row
"Di"
Output:
a!map(name: "Di", email: "", city: "")
Example 5: A trailing comma in the header
Input: ri!header
"name,city,"
Input: ri!row
"Ed,Goa"
Output:
a!map(name: "Ed", city: "Goa")
Appian Expression Tips
- • Use
ri!variableNamefor rule inputs - • Use
a!localVariables(local!x: …, body)for locals - • Use
a!forEach(items: …, expression: fv!item)to iterate - • Use
where()/reject(fn!isnull, list)to filter