FOREACH statement

Use the FOREACH statement to write statements in the probe rules file language that iterate through lists of event elements or table entries.

Syntax

The syntax of the FOREACH statement is as follows:

foreach (iterator in list)
{
	statements
}

In this syntax, iterator represents the item to be identified by the statement. iterator can consist of any combination of printable ASCII characters, and must start with a letter. For example: letter {letter|digit}.

list represents the elements to be processed in the rules file. list can be a comma-delimited list of elements or an array. You can use $* to instruct the loop to process all elements. The statement processes the elements in the list in a non-determinstic order.

statements represents valid probe rules file statements or functions that are to be applied to the elements in list

You can nest FOREACH statements inside each other. The FOREACH statement supports IF and SWITCH statements in the body of a loop.

In a statement the iterator represents the current loop item. Referencing its value depends on the type of list that is being processed.

When looping through a list of elements, note the following information:

  • You must prefix the iterator with $ to reference the current element.
  • If used on its own, the iterator represents the name of the current element.

When looping through an array, note the following information:

  • You must substitute the iterator for the key in referencing an array item.
  • On its own, the iterator represents a string that is the key to the current array item.

Supported probe rules file functions

All probe rules file functions are supported in a FOREACH statement.

Restrictions

You cannot use the FOREACH statement to iterate through properties, fields, or columns. You also cannot use the statement to iterate through a combination of arrays and elements in the same loop.

If you use the details() function in a FOREACH loop, only the result of the last-executed details() function are stored in the ObjectServer. Additionally, because the FOREACH statement processes the elements in the rules file in a non-determinstic order, it cannot be predicted which element is stored.