PermaLinkNotesDomino 6: @For rocks!
posted Tuesday 4th, January 2005
 



Yesterday I needed to update a formula used to generate a list of choices for a field. The existing formula was 20 lines with various @Replace and @SubString, etc. This was necessary in earlier versions of Notes in order to parse the prefixes on each of the potential values. Fortunately, this client site is running ND6, so I was able to replace what would have been about 30 lines of code (after the modifications) with a few lines similar to what's shown here:

tAllValues := @Trim(@DbLookup( ""; ""; "myLookupView"; "myKeyField"; 2));
tWantedPfx := "Prefix1\\" : "Prefix2\\" : "Prefix3\\";

@For( n :=1; n <=@Elements( tAllValues ); n := n + 1;
 tList := @If( @Begins( tAllValues[n]; tWantedPfx ); tList : tAllValues[n]; tList ));
@Sort( @Trim(tList) )


It's a great feeling to be able to go into code that's been around for quite a while and take advantage of current features to dramatically ease the future maintenance. Life is good :)
Comments :
 
 

1. Posted by Rock - website09/09/2007 08:42 PM



Well, no good code goes unmodified. You can make it even shorter (and faster) using @Transform. Consider:

tAllValues := @Trim(@DbLookup( ""; ""; "myLookupView"; "myKeyField"; 2));
tWantedPfx := "Prefix1\\" : "Prefix2\\" : "Prefix3\\";

@Sort(@Trim(@Transform(tAllValues; "X"; @If( @Begins( X; tWantedPfx ); X; "" ))));

Try that out sometime

Rock




2. Posted by Joe Litton - website09/09/2007 08:42 PM



Rock,

That @Transform is awesome! I just tested it and it works like a champ. It has now been placed where I'd had the @For. Dang, looks like I've got some refresher studying to do!




3. Posted by Paul09/09/2007 08:42 PM



I wrote my first @For formula not too long ago and it was strange to use a loop in formula. I had to set the value of a field depending on if any known values were contained in a multi-value field.

values := "value1":"value2":"value3";

Flag := @False;

@For(n :=1; n<=@Elements(multivaluefieldname); n:= n + 1;@If(multivaluefieldname[n]=values;Flag:=@True;@Success));

FIELD otherfield := @If(Flag;"newvalue1";"newvalue2");

SELECT @All




4. Posted by david09/09/2007 08:42 PM



my in-house notes guru, Wendy, suggested using the new @nothing as well:

tAllValues := @Trim(@DbLookup( ""; ""; "myLookupView"; "myKeyField"; 2));
tWantedPfx := "Prefix1\\" : "Prefix2\\" : "Prefix3\\";

@Sort(@Transform(tAllValues; "X"; @If( @Begins( X; tWantedPfx ); X; @Nothing )));