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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Efficiency of variable formula

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a formula using the following syntax. The formula works without any problems but I would like to know if this is the most efficient way of writing it: -

whileprintingrecords;
global numbervar var1:=
if {FLD1}=100 and {FLD2}="A" then var1 + {FLD3} else var1;
global numbervar var2:=
if {FLD1}=100 and {FLD2}="B" then var2 + {FLD3} else var2;
global numbervar var3:=
if {FLD1}=100 and {FLD2}="C" then var3 + {FLD3} else var3;
global numbervar var4:=
if {FLD1}=100 and {FLD2}="D" then var4 + {FLD3} else var4;

Is there anyway that I can make all four variable declarations at the top together and then write one statement below to make the calculations?
 
Matt,

Like you've already observed, there isn't really anything wrong with your formula. Certainly not as far as efficiency is concerned.

I tend to write multi-variable formulas a little differently, simply because I like them to be easy to follow when someone else has to read them in my wake - but it's all a question of personal aesthetics really.

In the case of your formula, I'd probably have all the variables declared in the same section, like you point out, and then handle the assigning of the values to the variables with one If statement just to minimised the repetition of shared criteria (in this case, "{FLD1} = 100").

WhilePrintingRecords;

NumberVar Var1;
NumberVar Var2;
NumberVar Var3;
NumberVar Var4;

If {FLD1} = 100
Then
If {FLD2} = "A" Then Var1 := Var1 + {FLD3}
Else
If {FLD2} = "B" Then Var2 := Var2 + {FLD3}
Else
If {FLD2} = "C" Then Var3 := Var3 + {FLD3}
Else
If {FLD2} = "D" Then Var4 := Var4 + {FLD3};

Note I dropped the Global declarations. For me, typing in global is a waste of space, as non-declared variables are global by default anyway. Additionally, because I handled everything with a nested if, my version doesn't have the 'Else Variable' clause yours does - simply because the . Ordinarily, I like to include 'Else Variable's where possible, but this instance won't lose anything without it.

As far as speed is concerned, either is as good as the other.

All the best,

Naith
 
I got distracted quite a lot during that last post. Excuse all the typos. [ponder]
 
Naith,

Thank you for your response, that was exactly what I was after. However can I extend the question a bit and ask you why the following does not work/how I can do it?

At the moment when I try this the first "set" of variable calculations works but the second does not. I presume it has something to do with the way I'm nesting it.

I've reduced the number of variables from the original for ease and added two new ones.

Many Thanks.

Whileprintingrecords;
NumberVar Var1;
NumberVar Var2;
NumberVar VarA;
NumberVar VarB;

If {FLD1} = 100
Then
If {FLD2} = "A" Then Var1 := Var1 + {FLD3}
Else
If {FLD2} = "B" Then Var2 := Var2 + {FLD3}
Else If {FLD1} = 200
Then
If {FLD2} = "A" Then VarA := VarA + {FLD3}
Else
If {FLD2} = "B" Then VarB := VarB + {FLD3};
 
Matt,

The reason you're running into that problem here is because you're moving down and then UP levels in the same If statement.

Because of the way you've indented your statement, it's perfectly obvious to me exactly what you're trying to do. However, to Crystal, indentations don't mean jack. As far as processing this formula is concerned when it gets to:

"Else If {FLD1} = 200"

it won't be read as the Else of this If:

If {FLD1} = 100

but rather it will be read as the Else clause of this If:

If {FLD2} = "B" Then Var2 := Var2 + {FLD3}

For it to be the Else clause of the latter If, it must first have satisfied the opening criteria of the Mother If:

If {FLD1} = 100

...which of course, it can't.

Your formula needs to change so that it becomes either:

WhilePrintingRecords;
NumberVar Var1;
NumberVar Var2;
NumberVar VarA;
NumberVar VarB;

If {FLD1} = 100
Then
If {FLD2} = "A" Then Var1 := Var1 + {FLD3}
Else
If {FLD2} = "B" Then Var2 := Var2 + {FLD3};
If {FLD1} = 200
Then
If {FLD2} = "A" Then VarA := VarA + {FLD3}
Else
If {FLD2} = "B" Then VarB := VarB + {FLD3};

...or...

WhilePrintingRecords;
NumberVar Var1;
NumberVar Var2;
NumberVar VarA;
NumberVar VarB;

If {FLD1} in [100,200]
Then
If {FLD2} = "A" Then Var1 := Var1 + {FLD3}
Else
If {FLD2} = "B" Then Var2 := Var2 + {FLD3}
Else
If {FLD2} = "A" Then VarA := VarA + {FLD3}
Else
If {FLD2} = "B" Then VarB := VarB + {FLD3};

Hope you see what I mean. But if you don't, you know where I am.

All the best,

Naith
 
Naith,

The first example you give is exactly what I am after. I have just tried it and it works fine - so thank you. As you say the second Else I had in there (and a missing ;) were stopping it working.

Many Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top