Skip to content

Fieldindex Predefined Variable

Obsolete

This variable is obsolete.

In character-mode DataFlex, this variable was typically used to loop through all (or a selection of) fields in a given data file. In DataFlex, you can do the same thing using the sample code below.

integer iMaxFields iField
string sFieldVal
handle hoFile

Open Customer
move Customer.File_Number to hoFile

// how many fields are in the Customer data file?
Get_Attribute DF_FILE_NUMBER_FIELDS of hoFile to iMaxFields

// find Customer number 10
clear Customer
move 10 to Customer.Id
find eq Customer by Id

if (Found) begin
    // loop through all fields in current record of Customer file
    // start with field 1 (skip Recnum field)
    for iField from 1 to iMaxFields
        get_Field_Value hoFile iField to sFieldVal
        showln "Value of field " (String(iField)) ": " sFieldVal
    loop
end

The example above opens the Customer data file, determines how many fields are in the file using the DF_FILE_NUMBER_FIELDS database API attribute, finds the Customer record with id 10, then loops through all fields in the Customer file (skipping the Recnum field), retrieves the value of each field using the get_Field_Value command, and displays the values using the showln command.

Purpose

To provide indexed addressing of one or more fields in a database file.

Syntax

move
    int_val
to fieldindex

or

df_filename.field
&

The ampersand indicates that the field to be addressed is the field whose number is greater than that of df_filename.field by the value of the fieldindex Predefined Variable.

What It Does

A series of fields in a database file may be addressed by a single command in a loop in which the value of fieldindex is controlled. The field which is addressed in each pass is controlled by the value of fieldindex as an offset from the field identified as df_filename.field&. The ampersand (&) is literal and required.

Until it is used, the value of fieldindex is 0. Thus, until some other value is moved to fieldindex, df_filename.field& is the same as df_filename.field with no ampersand after it. If fieldindex is 1, then df_filename.field& refers to the next field after df_filename.field.

There are two primary ways of using fieldindex:

  1. To select randomly from a range of fields. For example, records in Database File merch might have four fields, as:

  2. merch.recnum

  3. merch.retail_price
  4. merch.dealer_price
  5. merch.distrib_price

Records in the customer file might have a terms field containing 1, 2, or 3 which determines which price the customer gets. The following would move the correct price to the variable cost:

move customer.terms to fieldindex
move merch.recnum& to cost
  1. To access a range of fields sequentially.
for fieldindex from 1 to 40
    showln merch.recnum&
loop

This example displays to the screen the contents of each of the first 40 fields of Database File merch except the record number.

Fields (and files) can be addressed from running programs by their numbers. This can be done by addressing commands to indirect_file.recnum. At runtime, the number of the desired field must be moved to fieldindex and the number of the desired file to filenumber before the command(s) addressed to the symbol is executed.

Notes

  • Fieldindex may be positive, for a downward offset from df_filename.field&, or negative, for an upward offset.
  • If a value of fieldindex is generated which addresses a nonexistent field, DataFlex Error 77 (Field number out of range) will be generated.
  • A fieldindex command can address fields whose data types are different from that of df_filename.field. In most cases, this causes no problem, but the operation of some commands, like move, depends on the type of the field (String, Numeric, Date) and will malfunction where a field of type different from that of df_filename.field is addressed. Especially where data is being moved, it is good practice to address only fields of a single data type.
  • If a fieldindex command addressing multiple data types is required for moving data, use the movestr command. Since all data types can be represented as a string, data can be moved from fields intact with this command.
  • The ampersand character can't be a field name by itself; it must be appended to the actual name of a field in the file's definition.
  • A similar variable that works on windows instead of fields is windowindex.