Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Of if's, else's and for's

Status
Not open for further replies.

EdwinGene

Programmer
Sep 2, 2003
154
US
I am using Crystal 8.5 and Crystal Syntax for formulas. I declare the following variables:

Local numberVar intIndex;
Local stringVar strResult := "";
Local stringVar strSymbol := "";

The following syntax generates no errors:

if {?DataType} = "N" or {?DataType} = "S" then
if {?Result}[intIndex] in ".0123456789" then
strResult := strResult & {?Result}[intIndex]
else
strSymbol := {?Result}[intIndex]
else
strResult := ""

Whereas the following syntax generates the error "A boolean is required here.", and places the cursor just before the "str" of "strResult" in the "else" clause:

if {?DataType} = "N" or {?DataType} = "S" then
for intIndex := 1 to Length({?Result}) step 1 do
if {?Result}[intIndex] in ".0123456789" then
strResult := strResult & {?Result}[intIndex]
else
strSymbol := {?Result}[intIndex]
else
strResult := ""

And the following syntax generates the error "A string is required here.", and places the cursor just before the "f" of "for":

if {?DataType} = "N" or {?DataType} = "S" then
strResult := ""
else
for intIndex := 1 to Length({?Result}) step 1 do
if {?Result}[intIndex] in ".0123456789" then
strResult := strResult & {?Result}[intIndex]
else
strSymbol := {?Result}[intIndex]

It looks as though you cannot have a "for" loop nested inside an "if" statment.

Is there a mistake in my syntax, or do Crystal 8.5 formulas not allow "for" loops to be nested inside "if" statements?

Thank you.
 
For statements in Crystal syntax require a paren ( after the DO and a terminating one at the end, as in:

if {?DataType} = "N" or {?DataType} = "S" then
for intIndex := 1 to Length({?Result}) step 1 do(
if {?Result}[intIndex] in ".0123456789" then
strResult := strResult & {?Result}[intIndex]
else
strSymbol := {?Result}[intIndex]
)

And this part is redundant since you've already set it to "":

else
strResult := ""

-k
 
Just tried that and it did not help. Still got the same error messages.
 
First...though off topic...I would not use "local" in variable declarations unless you want it specific for that formula (a rarity)

*****************
Local numberVar intIndex;
Local stringVar strResult := "";
Local stringVar strSymbol := "";

if {?DataType} = "N" or {?DataType} = "S" then
for intIndex := 1 to Length({?Result}) step 1 do
if {?Result}[intIndex] in ".0123456789" then
strResult := strResult & {?Result}[intIndex]
else
strSymbol := {?Result}[intIndex]
else
strResult := ""


************************

I would write the formula as follows

WhilePrintingRecords;
numberVar intIndex;
stringVar strResult := "";
stringVar strSymbol := "";

if {?DataType} = "N" or {?DataType} = "S" then
(
for intIndex := 1 to Length({?Result}) do
(
if {?Result}[intIndex] in ".0123456789" then
strResult := strResult & {?Result}[intIndex]
else
strSymbol := {?Result}[intIndex];
);
);

This should work...although I question the usefulness of strSymbol...this value is constantly being overwritten and will have the last value of the piece of {?result) that isn't in the string...but maybe that is what you want.

The "else" is isn't necessary since you have already defined strResult as beng := "" in the declaration.





Jim Broadbent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top