Foreach loop
  > Home
v User Guide
    > Introduction
    > Hello World
    v Flow Control
       o If-else
       o Switch
       o Foreach loop
       o While loop
       o For loop
    > Function Calls
    > Authentication
    > Database access
    > Content Management
    > Remote Calling
    > Object-Oriented
    > Other Features
> Reference
> Portal Object
> Development Guide

sys Class
debug Class
Intrinsic Conversions
>> User Guide >> Flow Control >> Foreach loop <=  =>      <  1  >  
Foreach loop

Command "foreach" is so powerful that it has been the only loop structure in the pre-released version of BEE (until "while" and "for" were added later as they are part of the release requirements.)

var eat = "(array)Monday=>rice,Tuesday=>spaghetti,Wednesday=>no food left";

foreach (eat) display "On {foreach:key} we have {foreach}.<br>\n";

// Output:

// On Monday we have rice.

// On Tuesday we have spaghetti.

// On Wednesday we have no food left.


The array "eat" is called the loop source and the variable "foreach" is called the loop variable.

The loop source can be other structure.  For example, you can use "foreach" to access a database retrieval.  (It is in fact the most common way in BEE to process data record set sequentially.)

database prod query="select * from Product";

foreach ((db)prod) {
   display "{foreach:Name} ({foreach:Code}) costs ";

   display "{foreach:Price} per {foreach:Unit}<br>\n";



To take it further, the loop variable "foreach" can be replaced by a more meaningful name like "prodrec".  Also, there is an auto-counter in system variable result%loopvar:iteration, which will increase by one in each iteration (loopvar is the loop variable name.)  If you want to limit the number of iterations, you can specify the "maxiter" parameter before the "foreach" bracket.

To make this example a bit more complicated (and therefore interesting), if you find an error in the database access, exit the loop.  And if the Price contains 0, skip the record.  Let's see how all these can be done:

database prod query="select * from Product";

foreach maxiter=10 ((db)prod as prodrec) {

   if ('{status%database}' != 0) break;

   if ('{prodrec:Price}' == 0) continue;

   display "Product {result%prodrec:iteration}: ";
   display "{prodrec:Name} ({prodrec:Code}) costs ";

   display "{prodrec:Price} per {prodrec:Unit}<br>\n";



To prevent an accidental endless "foreach" loop, "maxiter" is default to 1000.  If you want the loop to go until the end regardless, set maxiter to 0.

foreach () display "{result%foreach:iteration}\n";


The above will display a sequence of numbers from 1 to 1000.  The empty bracket is required even if you have no loop source and no loop variable to specify.

Please consult BEE Script User Reference - PDF for more features of the "foreach" loop.


Previous Page       Next Page

Accsoft Computer Technology Pty Ltd     ABN: 98 065 617 549
PO Box 892, Epping NSW 1710         Level 1, Epping Office Park, 242 Beecroft Rd, Epping NSW 2121, Australia
Tel: Sydney - (02)98691668     National - 1300-881668         Fax: (02)98691866
© Copyright 2003 Accsoft Computer Technology Pty Ltd