## FormCalc Expressions (Foreach)

A couple of weeks ago, I started a series of posts on FormCalc Expressions. The first ones I convered were the “If and For expressions”:http://blogs.adobe.com/formbuilder/2006/09/formcalc_expressions_if_and_for.html. This time, I thought I would explain the Foreach expression.

h2. Foreach ExpressionThe Foreach expression is quite similar to the For expression but is meant to be used to achieve a slightly different kind of iteration that may be better suited to your needs rather than just increasing/decreasing the value of a counter (as for a regular For expression). The difference is that the Foreach expression assigns a different value taken from an argument list (list of objects/values) to a variable for each iteration of the loop (and there’s one iteration for each argument in the list).h3. FormCalc Syntaxbc. foreach Variable in (ArgumentList) doExpressionListendforIn this syntax, there’s an _ArgumentList_. This is simply a comma-separated list of objects/values to assign to the _Variable_ on each iteration.Based on the syntax, you can do things like set a _total_ field’s value to the sum of _subTotal_, _shipping_ and _taxes_ fields:bc. foreach val (subTotal, shipping, taxes) dototal = total + valendforIn this example, each time the expression loops, the variable _val_ assumes the “identity” of a different object in the argument list: First, the subTotal field, then the shipping field and, finally, the taxes field.h3. JavaScript SyntaxFor comparison, here is the equivalent JavaScript examples (note the semicolons and curly braces):bc.. var arr = {0: subTotal, 1: shipping, 2: taxes};for (key in arr){total.rawValue += arr[key].rawValue;}p. (In this case, the curly braces are optional but I think this is better style.) It’s certainly more cumbersome in JavaScript using the For…In expression (which is the closest equivalent to FormCalc’s Foreach expression I could find) because it’s only meant for iterating through an object’s properties or an array’s keys (which is what my example is doing).