fieldIndex

fieldIndex (schema itemName -- ordinal)

Inputs

Outputs

Returns the selected item ordinal for a known, non-NIL Text item name in the dereferenced Struct schema.

Only the schema participates. Current value contents do not participate.

fieldIndex follows last-match name-selection rules. It therefore returns one selected ordinal, not every matching ordinal. A successful result is always a valid selected-item ordinal in the range 0 through itemCount - 1.

For Dict, that selected ordinal matches the same last-match field rule used by name-based field selection. Use fieldName to preserve the exact stored name at one ordinal and has for presence-only testing.

Behavior

Example

{} 0 {} [
  mixed: { 1 "" def 2 "" def 3 "x" def 4 "x" def };

  "-- fieldIndex returns the last match --" printCompilerMessage
  virtual emptyKey: mixed "" fieldIndex;
  emptyKey printStack _:;
  mixed emptyKey fieldName printStack _:;
  virtual xKey: mixed "x" fieldIndex;
  xKey printStack _:;
  mixed xKey fieldName printStack _:;

  "-- earlier matches still exist --" printCompilerMessage
  mixed fieldCount printStack _:;
  mixed 0 fieldName printStack _:;
  mixed 2 fieldName printStack _:;

  "-- Tuple unnamed item --" printCompilerMessage
  virtual tupleKey: (1 FALSE) "" fieldIndex;
  tupleKey printStack _:;
  (1 FALSE) tupleKey fieldName printStack _:;

  0
] "main" exportFunction

Expected Output During Compilation

-- fieldIndex returns the last match --
1
""
3
"x"
-- earlier matches still exist --
4
""
"x"
-- Tuple unnamed item --
1
""

See also