weight, weight profile

Prev Next

Function Names

weight, weight profile

Description

The function weight() returns the total number of base and member variables and also their sub-members. The 2nd function parameter (option) allows to fine-tune the specification in order to obtain the desired results. It is possible to weigh member variables only. In this case, the weighing begins at the level of the member variable and ignores the parental base and member variables.

The function weight profile provides more detailed weight information for every level in the variable tree. The base variable is level 0, its array and structure members are at level 1, their sub-members at level 2, etc. The value returned is a parameter set containing the weight fore every level found.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

1, 2

Parameters

No.TypeDescription
1
code
variable
:string
Variable name

This variable including all its members and sub-members will be weighed.

Opt. 2
input
string Option

The following options are supported to calculate the weight:

all All variables will be counted, including base variable, both intermediate nodes (structure and array members als containing further sub-members) and leaf nodes (those not containing further sub-members).
valid Like 'all', but all base and member variables containing void values will not be counted
members Like 'all', but the base variable will not be counted
valid members Like 'members', but the base variable will not be counted
leaves Like 'all', but only the leave variables (without further sub-members variables) will be counted
valid leaves Like 'leaves', but only the leave variables containing valid data (without further sub-members variables) will be counted

Note:All data of any type will be counted as 1, regardless if is is just a boolean or numeric value, a very long string or a sophisticated parameter set.

Default value: all

Return value

TypeDescription
numeral Weight value

Number of base and member variables as specified in the function parameters

parameter set Weight profile

If weight profile is called, the the weight values are provided for every level as numerals in a paramter set. The size of the parameter set corresponds to the depth of the variable.

Examples

  dim( a[], 10, 0 ); // Level 1
  a[0] = null();

  dim( a[3], 5, 0 ); // Level 2
  a[3,0] = null();
  a[3,1] = null();
  structure( a[3,2], { Ha, He, Hi, Ho }, { 1, 2, 3, 4 } ); // Level 3

  see( a[] );

  for all parameters( { all, members, valid, valid members, leaves, valid leaves }, option[] )
      echo( option[], '  --  weight: &tab32;', weight( a[], option[]), "   Profile: ", weight profile( a[], option[]) );

  // Note: '&tab32;' is a B4P specific HTML like entity to advance to any tab position between 1 and 300.  Here position 32 is chosen.

Output

a[]                     [Void]                     (void,full access)
Array [   0]            [Void]                     (void,full access)
Array [   1]            0  "0"                     (numeral,full access)
Array [   2]            0  "0"                     (numeral,full access)
Array [   3]            0  "0"                     (numeral,full access)
  Array [   0]          [Void]                     (void,full access)
  Array [   1]          [Void]                     (void,full access)
  Array [   2]          0  "0"                     (numeral,full access)
    Ha                  1  "1"                     (numeral,full access)
    He                  2  "2"                     (numeral,full access)
    Hi                  3  "3"                     (numeral,full access)
    Ho                  4  "4"                     (numeral,full access)
  Array [   3]          0  "0"                     (numeral,full access)
  Array [   4]          0  "0"                     (numeral,full access)
Array [   4]            0  "0"                     (numeral,full access)
Array [   5]            0  "0"                     (numeral,full access)
Array [   6]            0  "0"                     (numeral,full access)
Array [   7]            0  "0"                     (numeral,full access)
Array [   8]            0  "0"                     (numeral,full access)
Array [   9]            0  "0"                     (numeral,full access)

all  --  weight:               20   Profile: {1,10,5,4}
members  --  weight:           19   Profile: {0,10,5,4}
valid  --  weight:             16   Profile: {0,9,3,4}
valid members  --  weight:     16   Profile: {0,9,3,4}
leaves  --  weight:            17   Profile: {0,9,4,4}
valid leaves  --  weight:      14   Profile: {0,8,2,4}
Try it yourself: Open LIB_Function_weight.b4p in B4P_Examples.zip. Decompress before use.

See also

depth