ParseExpression
Release: |
4.6 • 5.0 • 5.1 • 5.2 • 5.3 • 5.4 • 6.0 • 6.1 • 6.2 • 6.3 • 6.4 • 6.5 |
---|
new to Analytica 5.2
ParseExpression( text, badVal, nsContext )
Parses «text» into a parsed Analytica expression if possible. Non-text values (numbers, dates, null, etc) are passed through directly. If «text» is text and does not parse, «badVal» is returnedand the error message and error number are returned as the second and third return values, which you can optionally capture.. ParseExpression(t,t)
passes through non-parsing text. No error is issued and evaluation does not stop when «text» does not parse.
Local variables in the lexical context of the call to ParseExpression are not in scope for the parse.
The result is an Analytica parse tree. This is a special data structure that acts as an atom for array abstraction.
New to Analytica 6.5: Suppose there are two variables that both have the identifier X, but which live in different Namespaces. Then
ParseExpression( "X+1" )
would differ depending on where it appears. By default it resolves the identifier X
from the namespace of the object containing the expression. In some esoteric situations, you may need to interpret the expression from a different namespace context. You can do this by first obtaining a handle to the other namespace module, or to any object inside that namespace, and passing that handle to the «nsContext» parameter.
ParseExpression( "X+1", nsContext: Handle("NS2") )
Uses
Suppose you import a set of distribution specifications from an external source, such as a spreadsheet. These come in as text, such as "Uniform(0,1)". If you simply assign an array of these to a variable, the resulting edit table will have the text "Uniform(0,1)" instead of an expression -- i.e., there will be an extra set of quotes in the cell. To automate this process, you can parse the incoming cells before assigning, e.g.,
Assessments := ParseExpression( incomingAssessments, incomingAssessments)
The edit table cells will then contain the actual expression rather than quoted text.
Capturing the error messsage
Requires Analytica 6.4 or later.
You can access the error message and error number by capturing the second and third return values.
Local ( parsed, errorMsg, errorNum ) := ParseExpression( expr ) Do ...
Enable comment auto-refresher