@For iterative loop problem

Problem: @For loop will not add entries to a text list on each loop.

Steps to reproduce:

Create a radio button field on a form

Set the field choices to ‘use a formula for choices’

Use this formula:

choices := “Domino¥Add-Ons”:

       "Domino¥Cross Certs":

       "Windows¥3rd Party Software":

       "Windows¥FTP Transfers":

       "Windows¥Drive Mappings":

       "Network¥DNS Entries":

       "Network¥Firewall Rules":

       "Network¥NAT Rules":

       "Users¥Notify":

       "Other¥SMTP Exceptions";

Category1 := “Domino”;

@For(n := 0; n <= @Elements(choices); n := n + 1;

temp0 := choices[n];

@Prompt([Ok]; “choices[n]”; temp0);

temp1 := @If(@Contains(temp0; category1); temp0; “”);

@Prompt([Ok]; “temp1”; temp1);

category2 := @If(temp1 = “”; category2;

category2 = “”; temp1; category2 : temp1);

@Prompt([Ok]; “building category2”; category2)

);

temp2 := @Implode(category2; “#”);

@Prompt([Ok]; “category2”; temp2);

category2

Subject: Thanks for the help

The problem was I was referencing the text list with an element 0. Text lists indexes start at 1.

so here is working example code:

choices := “Domino¥Add-Ons”:

“Domino¥Cross Certs”:

“Windows¥3rd Party Software”:

“Windows¥FTP Transfers”:

“Windows¥Drive Mappings”:

“Network¥DNS Entries”:

“Network¥Firewall Rules”:

“Network¥NAT Rules”:

“Users¥Notify”:

“Other¥SMTP Exceptions”;

Category1 := “Domino”;

@For(n := 1; n <= @Elements(choices); n := n + 1;

temp0 := choices[n];

@Prompt([Ok]; “choices[n]”; temp0);

temp1 := @If(@Contains(temp0; category1); temp0; “”);

@Prompt([Ok]; “temp1”; temp1);

category2 := @If(temp1 = “”; category2;

category2 = “”; temp1; category2 : temp1);

@Prompt([Ok]; “building category2”; category2)

);

temp2 := @Implode(category2; “#”);

@Prompt([Ok]; “category2”; temp2);

category2

Here is actual code used on a radio button “use formula for choices” field:

choices := @DbLookup(“”:“”;“”:“” ; “(keywords)”; “Categories”;2);

@If(Category1 = “”; @Return(“Select Category First”); “”);

@For(n := 1; n <= @Elements(choices); n := n + 1;

temp0 := choices[n];

temp1 := @If(@Contains(temp0; category1); @Right(temp0; “¥”); “”);

cat2 := @If(temp1 = “”; cat2;

cat2 = “”; temp1; cat2 : temp1)

);

cat2

Subject: Here’s a bit better way

choices := @DbLookup(“”:“”;“”:“” ; “(keywords)”; “Categories”;2);

@If(Category1 = “”;

"Select Category First");

@Transform(choices; "x"; @If(@Begins(x; Category1+"¥"); @Right(x; "¥"); @Nothing)

)

Or even

@Trim(@Right(“¥”+choices; “¥”+Category1+“¥”))

Subject: There is at least one errors in your code

To do the long story short:

a) You start your loop from 0 but end with the number in @Elements => this is one to many (and it takes careful consideration for all the commands used which end is to be adjusted - as to if to start the loop from 0 or 1).

b) From what I know (started doing domino with R3 Beta about 15 years ago) , there is just one good way to display a variable which might contain an in-expected result - With other words, It works very well for me to do it only this way every single time for years now when searching for an error:

@Prompt([OkCancelList]:[NoSort]; “Dialog 1”; “” ; “” ; @Text(category2));

If you would have done so, you would have gotten a more or less understandable error message with an hint on Problem ‘a)’. This is quite helpful in many cases, as for the results of @DBLookup or something, too.

c) There is no need for a loop to do what you most likely wanted to do. Have a look at the following code:

choices := “Domino¥Add-Ons”:

       "Domino¥Cross Certs":

       "Windows¥3rd Party Software":

       "Windows¥FTP Transfers";

category1 := “Domino”;

category2 := category1 + @Trim(@Word (choices + category1; category1 ; 2));

@Prompt([OkCancelList]; “building category2”; “” ; “” ;category2);

However, this would fail, if “Domino” could be within the values right of Domino¥ - like if Choices contains something like

“Domino¥Cross Domino Certs”:

To get around this problem, use this instead (just did not want to start with this, as it is harder to understand (not tested, but I gues you get the idea)

_Separator := “¥”;

category1 := _Separator + “Domino” + _Separator;

category2 := category1 + @Trim(@Word ( (_Separator + choices) + category1; category1 ; 2));

In theory the problem does still exist, but usually a category like this is not to be expected:

“Domino¥Cross ¥Domino¥ Certs”:

One further Tip: If you ever want to report an error and want someone to look at it, try to do so with as little as possible and as obvious code as possible (no you did not do so).

Subject: First element of a list is index 1

In formula language, the first element of a list is index 1, so your loop should start with n:=1.

I.e. the first line of your @For loop is currently:

@For(n := 0; n <= @Elements(choices); n := n + 1;

Change that to:

@For(n := 1; n <= @Elements(choices); n := n + 1;