Parsing error with if-else statement in Data Function

Parsing error with if-else statement in Data Function

book

Article ID: KB0081571

calendar_today

Updated On:

Products Versions
Spotfire Analyst All supported versions

Description

Why do I get a parsing error when calling if-else statements in my Data Functions?

Example:

   if (2+2 ==4) print("true")
   else print("not true")

runs from an S+ script window, but reports an error if run in a Spotfire Data Function:

   Problem in parse(n = n, file = file): Syntax error: illegal name ("print")
   after "else" on input line 2,
   file "c:/documents and settings/ajack/desktop/script1.ssc"
   Use traceback() to see the call stack

Symptoms:
Parsing error when running data function

Cause:
Parsing differences between S+ and Spotfire Data Functions

Issue/Introduction

Parsing error with if-else statement in Data Function

Resolution

The parsing of if-else statements differs if running from an S+ script window to running from a Spotfire Data Function. When run from within a Data Function, the "else" part of the if-else *must* be on the same line as the end of the "if" part.

For example:

   if (2+2 == 4) {print("true")
   } else print("not true")

runs OK whether directly in S+ or from within a Spotfire Data Function, since the "else" is on the same line as the closing curly bracket of the "if" clause.