16.2 Foreach Loops

16.2.1 Synopsis

foreach (item in array_name [ ])
  statement;

16.2.2 Description

Statement is executed once for every element in array_name. Inside statements block, current array element is available as variable item.

There are other ways to code foreach loops; as an example,

foreach (item in array_name)
  statement;

is perfectly correct. However, since array_name can only be an array, I strongly suggest to use the array_name [ ] notation (Section 12.1).

If array_name [ ] is a multidimensional array, item will itself be an array so you may find valuable the following form:

foreach (item [ ] in array_name)
  statement;

If foo [ ] contains both arrays and scalar values you must use item instead:

foreach (item in array_name)
  statement;

item can store both scalars and arrays while item [ ] can only store arrays.

You can also execute more than one statement inside the loop: simply enclose them between braces:

foreach (item in array_name [ ]) {
  statement;
  statement;
}

Remember that whitespaces don't affect Cows' behavior so the following forms are misleading:

foreach (line in quote [ ])
  echo (line); print ("\n");
foreach (line in quote [ ])
  echo (line);
  print ("\n");

Both forms suggest that for every element in array quote [ ] both line and \n are printed but Cows doesn't care about formatting so they are equivalent to:

foreach (line in quote [ ])
  echo (line);
print ("\n");

line is printed for every element of array but \n is printed only once at the end of the foreach loop. If that's not what you probably wanted, the correct ways to write the code above are:

foreach (line in quote [ ])  {
  echo (line); print ("\n");
}
foreach (line in quote [ ])  {
  echo (line);
  print ("\n");
}

The second form is much more readable.

16.2.3 Example

The following lines define an array and display its elements:

quote_array [ ] = { "The skin of a python is",
                    "no less precious to the snake",
                    "than fur is to the fox.",
                    "-- Maneka Ghandhi" };

foreach (line in quote_array [ ])
  print (line);

This is Cows' output:

The skin of a python is
no less precious to the snake
than fur is to the fox.
-- Maneka Ghandhi

16.2.4 Example 2

A site about vegetarianism consists in three sections; each section is represented by a label and a descriptive name grouped in an array:

{ "hunger", "Vegetarianism and World Hunger" }

The whole structure is stored in a multidimensional array:

site.topics [ ] = {
  { "suffering", "The suffering behind meat" },
  { "health", "Vegetarianism and Health" },
  { "hunger", "Vegetarianism and World Hunger" }
};

This array will be used through the site to create navigation bars, site maps etc. The tutorial introduction (Part II - Tutorial shows some examples of this technique).

Now, we add the following lines right after the definition of site.topics [ ]:

for (i=0; i<length (site.topics [ ]); i++) {
  site.topics[i][2] = '<a href="' + site.topics [i][0] + '.html">'
                      + site.topics [i][1] + '</a>';
}

In practice, for each section, we build a link pointing to an html file named after section's label, and add it to each array. As an example, the third array will store the following elements:

Important: The following code wouldn't work:

foreach (topic [] in site.topics []) { // *** WRONG ***
  topic [2] = '<a href="' + site.topics [i][0] + '.html">'
              + site.topics [i][1] + '</a>';
}

On each iteration the foreach loop copy an element of array site.topics [ ] into topic [ ]. So, inside the loop, we wouldn't add the element to site.topics [ ] but to a temporary array topic [ ]; site.topics [ ] wouldn't be affected.

This manual can be downloaded from http://www.g-cows.org/.