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

@While loop with array

Status
Not open for further replies.

bpeirson

Technical User
Sep 28, 2005
85
0
0
CA
Hi,

I'm working with Lotus Notes 9 (IBM Notes).
I'm trying to check for a value in a series of fields using a loop. There may be an obvious answer here but I can't find it.

Code:
n := 1;
@While(n <= @Elements(myNames);
@If( FIELD(myNames[n]) ="4" ; fullNote := fullNote + ", " + myNames[n] ; );
n := n + 1);

The FIELD expression is giving me trouble. How do I refer to a field using an array of field names?

A test of the loop successfully added all the array elements to the "fullNote" variable and displayed them. Adding the FIELD() reference caused the failure.
 
Greetings,

Generally, using FIELD is done like so : FIELD fname := x;

FIELD is used for affecting a value to a document field. You are comparing, so you don't need it. When you compare field content, just using the name of the field works fine.

I have never needed to work with an array in Formula, I prefer using LotusScript for that, but I would suggest the following modification :

Code:
n := 1;
@While(n <= @Elements(myNames);
@If( myNames[n] ="4" ; fullNote := fullNote + ", " + myNames[n] ; );
n := n + 1);

Let me know how that goes :)

Pascal

I've got nothing to hide, and I demand that you justify what right you have to ask.
 
Pascal, bpeirson here. I can't access my first account so I set up an new account so I can respond to my own thread. Go figure!

You're proposed solution doesn't work. Suppose my first field's name is "firehazard" and it contains "4".
When I use "firehazard" in the code the system recognizes it as a field and compares the field's value to "4" in the @IF statement resulting in the true condition.
When I use "myNames[1]" the system recognizes it as an array variable and compares the variable's first element, "firehazard", to "4" in the @IF statement resulting in the false condition.

Excel has the INDIRECT function which tells the system that the variable contents cited are to be treated as a range. Does Lotus notes have a similar function or procedure to pass a variable value as a field name?

I'm hoping you know a solution to this problem. Thanks.
 
I have just made a test scenario. In an empty database I created a form. On the form I created two fields, myNames and test.
In the myNames field (number, multivalue), I gave it a default of 1:1:1 - meaning three values of 1.
In the test field, I created a translation formula as follows :

Code:
n := 1;
a := 0;
@While(n <= @Elements(myNames);
a := a + myNames[n];
n := n + 1);
a

When I refresh the document, the test field gets filled with 3, which is the correct answer and demonstrates that the code works.
I'm sure you can adapt this example to your scenario.

Pascal.

I've got nothing to hide, and I demand that you justify what right you have to ask.
 
Agreed, your code works as noted.

In your example myNames is the name of the database field and the results are as expected.

In my case myNames is the name of an array variable within a report. The report is pulling values from the database where there is no field named myNames. The database is not one that I made, nor am I able to revise it so that I can use your code to get the information I need in the way you have done.

To further illustrate what is happening when my report runs, see below. I have shown a short version of the myNames array which will contain 54 different field names.

Code:
myNames := "Firehazard" : "Fireresponse";
n := 1;
@While(n <= @Elements(myNames);
@If( [u]myNames[n][/u] ="4" ; fullNote := fullNote + ", " + myNames[n] ; "");
n := n + 1);

At n=1 the @IF statement is comparing the array value of myNames[1], "Firehazard", to the string value "4". Condition false.
At n=2 the @IF statement is comparing the array value of myNames[2], "Fireresponse", to the string value "4", etc. Condition false.
The value in the fields of the database may be any one of the following, "X", "1", "2", "3", "4", "5", "6", "7", "8" or "9".

I am hoping to loop the @IF statement so I don't need to repeat it manually for the 54 different fields I want to check. The conditions where the field value is any of "1", "2", "3" or "4" needs to be identified by the code therefore I will need 116 instances of the @IF statement unless I can iterate it in a loop.

Ultimately I need a function that will allow the code to use the underlined instance (in the code above) of myNames[n] as a field reference where the value of myNames[n] is the field name to get the values from.
 
Pascal,

I found the function I need, @GetField.

Code:
n := 1;
@While(n <= @Elements(myNames);
@If( [u][b]@GetField[/b][/u](myNames[n]) ="4" ; fullNote := fullNote + ", " + myNames[n] ; "");
n := n + 1);

Thanks for your help. Your time is appreciated.
 
I didn't realize that myNames contained the name of a field you needed to check the value of. Obviously, GetField is your solution.
If I had realized that, I would have pointed you there sooner.
I'm glad you solved your problem. Given the scenario you outline, I would have definitely done it in LotusScript :).
In any case, happy coding !

Pascal.

I've got nothing to hide, and I demand that you justify what right you have to ask.
 
If I need more help it is good to know someone is paying attention in this group. Thanks.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top