3.78 DM_ValueCount

Returns the number of values in a collection (without the default values). It is also possible to return the index type or the highest index value.

The returned value indicates the number of values (without the default values). Thus, in combination with the DM_ValueIndex function, loops over all indexed values respectively elements can be implemented easily.

Depending on the value parameter, the following results may occur:

value Type

retvalue Return Type

Remark

DT_refvec, DT_list, DT_vector

DT_integer

Highest index value

DT_matrix

DT_index

Highest index value

DT_hash

DT_datatype

Any index type (anyvalue)

otherwise

DT_void

Non-indexed value

DM_UInt DML_default DM_EXPORT DM_ValueCount
(
  DM_Value   *value,
  DM_Value   *retvalue,
  DM_Options  options
)

Parameters

-> DM_Value* value

This parameter refers to the value reference from which the number of values is fetched. It should be a managed value reference or function argument.

-> DM_Value * retvalue

If this parameter is not NULL, the count value is returned in it. This may be a managed value reference, however this is not mandatory.

-> DM_Options options

These are the options available:

Option

Meaning

DMF_GetLocalString

This option means that text values (IDs of type DT_text) should be returned as strings in the currently set language.

DMF_GetMasterString

This option means that text values (IDs of type DT_text) should be returned as a strings in the development language, regardless of which language the user is currently working with.

DMF_DontFreeLastStrings

Strings are usually passed to the application in a temporary buffer, which is retained until the next call to the IDM. If strings in the application shall be valid longer, the option DMF_DontFreeLastStrings has to be set. Then the memory will not be released until an IDM function returning a string from the IDM to the applicationis called without this option.

Return value

0 … INT_MAX

Number of values (excluding the default values with the indexes [0], [0,*] or [*,0]).

retvalue

Highest index value, may be either void (scalar value), an integer value (one-dimensional array), an index value (two-dimensional array), or a data type (associative array).

Example

Dialog File

dialog YourDialog
function integer CountIntegers(anyvalue List);

on dialog start
{
  variable matrix M := [
    [0,0]=>-1,[1,1]=>"ZIP",[1,2]=>"City",[2,1]=>60654,[2,2]=>"Chicago" ];
  print "#Integers in Hash: " + CountInteger(M);
  exit();
}

C Part

...

static DM_Value InvalidIndex;
static DM_Value InvalidValue;

DM_Integer DML_default DM_ENTRY CountInteger(DM_Value *List)
{
  DM_Value   index;
  DM_Value   count;
  DM_Value   value;
  DM_Integer icount = 0;

  /* initialize the managed values */
  if (DM_ValueCount(List, &count, 0)>0 && List->type == DT_matrix
      && count->type == DT_index)
  {
    index.type = DT_index;
    index.value.index.first = 0;
    index.value.index.second = 1;

    /* loop through [0,1],[1,1],[2,1],... */
    while(index.value.index.first<=count.value.index.second)
    {
      if (DM_ValueGet(List, &index, &value, 0)
          && value.type == DT_integer)
        icount++;
      index.value.index.first++;
    }
  }
  return icount;
}

Availability

Since IDM version A.06.01.a

See also

Functions DM_ValueChange, DM_ValueGet, DM_ValueIndex

Built-in functions countof(), itemcount()