for all variables

Prev Next

Function Names

for all variables, for all variables if existing, for all variables if existing and valid

Synopsis

for all variables ... ( base variable name, member variable value, iterator counter, member variable name ) statement;
for all variables ... ( base variable name, member variable value, iterator counter, member variable name ) {statements; }

Description

The loop works through all direct member variables specified in the base variable provided in the 1st parameter.

The base variable provided may either be an array or a structure. The loop will be skipped if the specified variable has no members. The loop will not step into nested variable sub-members. The function for all variables if existing will not assert an exception if the base variable has not been defined and will also skip the loop.
The function for all variables if existing and valid works similar to the function above, but skips all variables containing void values.

Call as: procedure

Restrictions

Indirect parameter passing is disabled

Parameter count

2-4

Parameters

No.TypeDescription
1
code
variable
:string
base variable name

The loop will be executed if the variable is either a structure or an array containing members.

2
code
variable
:string
member variable value

This variable is updated by this function in order to provide the value of the current variable. It is a copied value. Modifying this variable will not influence the loop

Opt. 3
code
variable
:string
counter

This variable is updated by this function and provdes the index. It always begins with 0 and continues with 1, 2, etc.

Opt. 4
code
variable
:string
member variable name

If the base variable is a structure, then this variable is updated with the name of the member variable. However, if it is an array, then the value "# Array Member #" will be assigned. This fourth parameter is not meaningful for running through arrays.

Examples

  structure( a[], { Animal, Plant, Alien, Nothing }, { Ape, Tulip, Chewbacca, null() } );

  echo(new line, "Output all structure members: ");
  for all variables( a[], value[], i[] , name[])
  {
      echo( i[],": ", name[]," = ", value[] );
  }

  echo(new line, "Output all valid structure members: ");
  for all variables if existing and valid( a[], value[], i[] , name[])
  {
      echo( i[],": ", name[]," = ", value[] );
  }

  array( a[], { Charles, Dave, Abel, null(), Paul, Birgit, } ); // One void value inside

  echo(new line, "Output all array members: ");
  for all variables( a[], value[], i[] , name[])
  {
      echo( i[],": ", name[]," = ", value[] );
  }

  echo(new line, "Output all valid array members: ");
  for all variables if existing and valid( a[], value[], i[] , name[])
  {
      echo( i[],": ", name[]," = ", value[] );
  }

  echo(new line, "Attempt to output a non-existing variable: ");
  for all variables if existing and valid( nonexising name[], value[], i[] , name[])
  {
      echo( i[],": ", name[]," = ", value[] );  // Nothing listed.
  }

  echo(new line, "Output array members with few of them defined: ");

  a[1] = 33;
  a[9] = Hello;
  a[20] = 1;
  a[40] = true;
  a[2] = null();

  for all variables if existing and valid( a[], val[], cnt[] )
  {
      echo( cnt[],": ", val[] );  // Only valid values listed.
  }

Output

Output all structure members:
0: Alien = Chewbacca
1: Animal = Ape
2: Nothing = # Invalid Value #
3: Plant = Tulip

Output all valid structure members:
0: Alien = Chewbacca
1: Animal = Ape
3: Plant = Tulip

Output all array members:
0: # Array Member # = Charles
1: # Array Member # = Dave
2: # Array Member # = Abel
3: # Array Member # = # Invalid Value #
4: # Array Member # = Paul
5: # Array Member # = Birgit

Output all valid array members:
0: # Array Member # = Charles
1: # Array Member # = Dave
2: # Array Member # = Abel
4: # Array Member # = Paul
5: # Array Member # = Birgit

Attempt to output a non-existing variable:

Output array members with few of them defined:
0: Charles
1: 33
4: Paul
5: Birgit
9: Hello
20: 1
40: true
Try it yourself: Open LIB_Function_for_all_variables.b4p in B4P_Examples.zip. Decompress before use.

See also

for all parameters

Notes

Manipulation while inside the loop:
1. The iterator cannot be manipulated. It will be overwritten at the next loop round.
2. If the iterator is deleted inside the loop, it will be reinstated at the next loop round. This applies to all loop variables.
3. If the target variable name is deleted, or its members are deleted / added, then the loop will end. If some of the members are removed, it will have an impact such as fewer loop cycles. Since the members are sorted in alphabetical order, the whole list of members may shift accordingly. Therefore I recommend not to manipulate this variable while inside the loop. Inside the loop: If the affected variable is deleted or a member variables removed, then the loop will do a premature end.