Changing specific values in a list

I am trying to validate a field with multiple values, to do this, i’ve created a querysave event that goes through each item in the list, and checks to see if it’s in an approved value… but I can not get it to work.

I have a multiple value text field called Fruits whose values are apple:carrot:banana

ideally i’d like to check each value on save and then replace the value if needed:

@For(n := 1;

n <= @Elements(Fruits);

n := n + 1;

@If(Fruits[n]=“carrot”; Fruits[n]:=“cherry”;“”)

)

but i get a “incorrect data type: text expected” error.

i’ve tried a bunch of different says of doing this (including a subset inside a@for loop) but none of them seem to work.

what is a simple way of doing this??!

TIA!

Subject: changing specific values in a list

You can read from a list index (val := list[i]), but you cannot write to one (list[i] := val). You can build a new list, or use @Transform.

OR – you can start to think in Formula Language instead of Basic or Java or what have you:

Fruits := @Replace(Fruits; “carrot”; “cherry”)

List operations are powerful and robust. Loops are nice and all, but they can be a tremendous waste of time and energy when there’s a simpler alternative available.

Subject: RE: changing specific values in a list

I suppose @Replace might work, but i was hoping to have a drop down box (e.g. do you want to replace value ??? with banana or cherry) and @replace would replace every instance (in the case where i had apple carrot carrot banana, one dialog would make it apple cherry cherry banana)

i tried building a new list:

@For(n := 1;

n <= @Elements(Fruits);

n := n + 1;

@If(Fruits[n]=“carrot”;

upperlist:= @Subset(Fruits;1)+

lowerlist:= @Subset(Fruits;-1)+

newvalue:=“cherry”+

@Prompt([Ok];“dialog box”;Fruits[n] +" is being changed to "+newvalue )+

finallist := upperlist:newvalue:lowerlist+

FIELD Fruits:=finallist;“”)

)

but it leaves a blank field. i think it’s because of the "+"s but when i put each in it’s own @if, it still didn’t work. Obviously i’m not all that great at this, it’s been killing me.

Subject: RE: changing specific values in a list

What’s with all the + things in that formula? And how would a dialog/prompt/dropdown interfere with using @Replace? If you want to replace all instances of x with y, @Replace is the formula you should be using .

Subject: RE: changing specific values in a list

the + symbol has allowed me to do multiple operations within a single @if, using them in place of semicolons. (since semicolons proceed to the next test) probably not the safest way to do it, but it often works and is a lot cleaner that having repeated @if statements (i’ve tried separating them out, but it doesn’t work for some other reason)

i hesitate at using replace because of this scenario:

apple carrot carrot banana

first carrot: “This is not a fruit, would you like to replace it with one of the following: Grape, kiwi, tomato”, the user then enters tomato

second carrot: “This is not a fruit, would you like to replace it with one of the following: Grape, kiwi, tomato”, the user then enters Grape

ending up with: apple tomato grape banana

(which I don’t think would be possible with replace. I’m assuming that is someone is dumb enough to put in a vegetable, then they won’t be consistent in their replacements)

I may just use @replace for lack of anything better though, so i do appreciate the response!

Subject: RE: changing specific values in a list

No, + doesn’t do that, it adds or concatenates the result of other formulas. It MAY do sequential operations as a side-effect of the addition/concatenation, but you are playing with fire because of function return data types. There is a function to do that – @Do – which does not depend on function returns, and is therefore type-safe.

It’s been my experience that when somebody enters the same value twice, it’s because they meant to enter the same value twice. Your mileage may vary.

Subject: RE: changing specific values in a list

well, that did it, i guess sometimes it’s the simplest things that are the hardest to grasp.

you have my undying appreciation and respect.

@For(n := 1;

n <= @Elements(Fruits);

n := n + 1;

@If(Fruits[n]=“carrot”;@Do(

upperlist:= @Subset(Fruits;1);lowerlist:= @Subset(Fruits;-1);

newvalue:=“cherry”;@Prompt([Ok];“dialog box”;Fruits[n]+" is being changed to "+newvalue );

finallist := upperlist:newvalue:lowerlist;

FIELD Fruits:=finallist);“”)

)

(In defense of my own ignorance, i don’t actually do this full time!) -Thanks again!