Difference between revisions of "ParseExpression"

(EW18664)
 
(Ew 21095 «nsContext»)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{ReleaseBar}}
 +
 
''new to [[Analytica 5.2]]''
 
''new to [[Analytica 5.2]]''
  
== ParseExpression( text, badVal ) ==
+
== ParseExpression( text'', badVal{{Release|6.5||, 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 returned. <code>ParseExpression(t,t)</code> passes through non-parsing text. No error is generated and evaluation does not stop when «text» does not parse.
+
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 returned{{Release|6.4|| and the error message and error number are returned as the second and third return values, which you can optionally capture.}}. <code>ParseExpression(t,t)</code> 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.  
 
Local variables in the lexical context of the call to [[ParseExpression]] are not in scope for the parse.  
Line 9: Line 11:
 
The result is an Analytica parse tree. This is a special data structure that acts as an atom for array abstraction.  
 
The result is an Analytica parse tree. This is a special data structure that acts as an atom for array abstraction.  
  
 +
{{Release|6.5||
 +
''New to [[Analytica 6.5]]'': Suppose there are two variables that both have the identifier X, but which live in different [[Namespaces]]. Then
 +
:<code>[[ParseExpression]]( "X+1" )</code>
 +
would differ depending on where it appears. By default it resolves the identifier <code>X</code> 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.
 +
:<code>[[ParseExpression]]( "X+1", nsContext: [[Handle]]("NS2") )</code>
 +
}}
  
 
== Uses ==
 
== Uses ==
Line 14: Line 22:
 
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.,
 
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.,
  
::<code>Assessments := ParseExpression( incomingAssessments, incomingAssessments)</code>  
+
::<code>Assessments := [[ParseExpression]]( incomingAssessments, incomingAssessments)</code>  
  
 
The edit table cells will then contain the actual expression rather than quoted text.
 
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.
 +
 +
:<code>[[Local]] ( parsed, errorMsg, errorNum ) :{{=}} [[ParseExpression]]( expr ) Do ...</code>
  
 
== See also ==
 
== See also ==
 
* [[ParseNumber]], [[ParseDate]]
 
* [[ParseNumber]], [[ParseDate]]
 
* [[ParseCSV]], [[ParseJSON]]
 
* [[ParseCSV]], [[ParseJSON]]
* [[ParsedExprFunction]], [[ParsedExprParameters]]
+
* [[ParsedExprFunction]], [[ParsedExprParameters]]{{Release|6.5||
 +
* [[Namespaces]]}}

Latest revision as of 22:57, 17 May 2024



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 ...

See also

Comments


You are not allowed to post comments.