Eq, Ne, Ge, Gt, Le and Lt (SelectionCriterion functions)

From m204wiki
Revision as of 03:24, 28 June 2012 by JAL2 (talk | contribs) (→‎Examples)
Jump to navigation Jump to search

Create selection expression based on comparison operator (SelectionCriterion class)


These shared methods each create a new SelectionCriterion object that is a relational expression used to select items from a collection. Each of these constructors provides a different comparison operator. The Eq method, for example, constructs an equality expression that selects a collection item if the expression is true for that item. The other methods construct, respectively, not-equal-to, greater-than-or-equal-to, greater-than, less-than-or-equal-to, or less-than expressions.

The collection searching method that makes use of a selection criterion specifies:

  • Whether to return the first item or item number or all items that satisfy the selection criterion.
  • Where in the collection to begin searching.

The first of the two parameters in a selection criterion specifies a function that is applied to a collection item. The function result is the "left-side" operand in the criterion's expression. The second selection criterion parameter is a string or numeric that is the "right-side" operand in the expression. Eq(name, 'Ortiz') specifies the expression name='Ortiz'.

The function parameter of one of these methods might simply be an identity function that returns the item value. Or, for example, it might be a function that returns the value of a class member for an item that is an object. This function must be a method that operates on the item type and returns a User Language intrinsic datatype (Float, String, Longstring, or Unicode) value.

Eq syntax

%selectionCriterion = [%(SelectionCriterion For itemType):]Eq( itemFunctionEnum, - result)

Syntax terms

%selectionCriterion A SelectionCriterion object variable to contain the new object instance.
[%(SelectionCriterion For itemType):] For a shared function, this optional specification of the class in parentheses denotes a virtual constructor. See "Usage notes", below, for more information about invoking a SelectionCriterion virtual constructor.
itemFunctionEnum A method value (a method name literal, a method variable, a class Variable, or even a method that returns a method value) for a method that operates on objects of the type specified on the declaration of the collection being searched, and that returns a User Language intrinsic (numeric, string, or Unicode) value.

Note: For the Eq and Ne methods only, the return value of this argument's function may also be an enumeration value, as of Sirius Mods Version 7.8. See Example 1 below.

result An intrinsic value or expression. For a string value comparison, character case does not matter.

Usage notes

    As described in "Virtual Constructor methods", Eq and the rest of these functions can be invoked with no method object, with an explicit class specification, or with an object variable of the class, even if that object is Null:

    %selCrit = Eq(itemFunctionEnum, result) %selCrit = %(SelectionCriterion for float):LE(itemFunctionEnum, result) %selCrit = %selCrit:Gt(itemFunctionEnum, result)

    Note: As shown in the second of these above, if you explicitly specify the class name, you must include the item datatype of the collection to be searched, just as on a SelectionCriterion object variable's declaration.

  • For more information about the itemFunctionEnum parameter, see "Specifying a SelectionCriterion's parameters".
  • The itemFunctionEnum parameter is a method value, not a User Language expression, and you may not specify a function that itself has an argument. The SelectionCriterion syntax does not provide for specifying a parameter for the itemFunctionEnum parameter. If necessary, the workaround for this restriction is to define a local method that accepts an argument, then use that method as the itemFunctionEnum parameter.
  • The itemFunctionEnum may be This, an identity method that is valid for User Language intrinsic method objects only. The This method returns the value of the item to which it is applied. This example on the "SelectionCriterion class" page uses This.

Examples

  1. The following criterion matches a number greater than 10:

    %sel = gt(this, 10)

    The following matches a number with an absolute value less than or equal to 100:

    %sel = le(absolute, 100)

    The following matches items whose error return value is a Boolean True:

    %sel = eq(error, true)

    The following matches numbers less than 70 and greater than or equal to 95:

    %sel = or(lt(this,70), ge(this,95))

  2. The following request fragment selects two items from the %fool NamedArraylist in the SubsetNew example:
         ...
        %sel is object selectionCriterion for object foo
        %sel = EQ(status, 'Employed')
        %fool = %fool:subsetNew(%sel)
    
        for %i from 1 to %fool:count
           print %fool:namebynumber(%i) And -
                 %fool:itembyNumber(%i):myprint
        end for
         ...
    

    The result is:

        Jan Green employed (income: 55000)
        Jim Red employed (income: 45000)
    

See also