Expressions and their Resolving
Expressions and their Resolving in CSVfox commands
General
An expression is a compilation of placeholders and literal text in one string.
Through resolving, all placeholders will be replaced, such as formulas will be computed, numbers will be formatted, or column content will be inserted.When done, the resolved expression, i.e. the completed text string, can be used for being inserted into a column field, for evaluating a comparison, or for which purpose ever.
Usage of Expressions
Expressions are ubiquitous in CSVfox commands.They are resolved at runtime for each individual CSV data row and for each individual CSV data field.
Expressions can be used
- as content to be assigned to a CSV field:
-new[GrossPrice]=([NetPrice]*[TaxFactor]) - as a condition for a command execution:
-if{...}//set[field]= ... - as a filter condition for the output of data rows to the output file:
-filter[Fruit]!c=Apple/Pear
As a condition or a filter, an expression, or a comparison of two expressions, yields a truth value that decides whether or not that condition is satisfied.
Resolving of an Expression
Resolving means doing all necessary replacement inside an expression.
This is one of the main functions of CSVfox, building the output data from input, according to your requirements.
Resolving works line-wise, which means that all (but only the) data of the current data row is available for processing.
When resolving happens on the input side (+set, +replace, +regex, +add etc.), only data of the currently read input or merge file is available. Resolving on the output side (-set, -replace, -regex, -add etc.) will happen afterwards and works with all data of the input and merge files, combined into a single data row for output.
Resolving may include:
- inserting well-known data, like the current line number for
(#) , or the original contents of the current field instead of[*] - transforming escaped control characters and code point placeholders to their desired values, see Placeholders
- interpreting, reformatting, and inserting numeric CSV data fields
- calculating formulas from numeric CSV data fields and expressions, and inserting the (optionally formatted) result
- and inserting all other CSV fields and variables as required.
Example: resolving a numerical expression
We have a simple CSV table about the prices of fruit on sale as an input file.
We want to display the gross sales price together with an advertising text. Unfortunately, we only have the net prices, but fortunately the table also contains the sales tax rate.fruit | net price | sales tax |
---|---|---|
apples | 2,00 | 19 |
pears | 3,00 | 19 |
This command adds a new column named "Special Offer" to the output file.
The part to the right of "=" is an expression. It consists of some text, then a formatted formula (which is calculated from two other columns), then text again.
Then, for each individual row, this is the expression that is assigned to the column content:
This expression must be resolved and the result become inserted into the new
As part of the expression we see a fomula that must be calculated and formatted first:
At first, the value of the
That value becomes embedded in a string that starts with "Only today: ", then comes the formatted number, then at the end is " Euro!".
That string will become the content of the new
And this will be the resulting output file:
fruit | net price | sales tax | special offer |
---|---|---|---|
Apples | 2,00 | 19 | Only today: 2,38 Euro! |
Pears | 3,00 | 19 | Only today: 3,57 Euro! |
You can find more about getting CSV field contents at CSV Field Access.
Escaping Special Characters
Hide special characters in text or field names from interpreting them as special
If you also use these special characters like "[", ")", etc. in literals text, they must be hidden from being interpreted.
You do this through prepending a backslash "\" before them.
Please find more details here: Character Escaping and Placeholders.