Eq, Ne, Ge, Gt, Le and Lt (SelectionCriterion functions): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
 
(12 intermediate revisions by 2 users not shown)
Line 8: Line 8:
The other methods construct, respectively, not-equal-to, greater-than-or-equal-to,
The other methods construct, respectively, not-equal-to, greater-than-or-equal-to,
greater-than, less-than-or-equal-to, or less-than expressions.
greater-than, less-than-or-equal-to, or less-than expressions.
The [[Collections#Searching a collection|collection searching method]]
that makes use of a selection criterion specifies:
<ul>
<li>Whether to return the first item or item number or all items that satisfy the
selection criterion.
<li>Where in the collection to begin searching.
</ul>


The first of the two parameters in a selection criterion specifies a function
The first of the two parameters in a selection criterion specifies a function
Line 31: Line 23:
This function must be
This function must be
a method that operates on the item type and returns a <var class="product">User Language</var> [[Intrinsic classes#Two generic intrinsic classes: string and numeric|intrinsic datatype]]
a method that operates on the item type and returns a <var class="product">User Language</var> [[Intrinsic classes#Two generic intrinsic classes: string and numeric|intrinsic datatype]]
(<var>Float</var>, <var>String</var>, <var>Longstring</var>, or <var>Unicode</var>) value.
(<var>Float</var>, <var>String</var>, or <var>Unicode</var>) value.


==Eq syntax ==
==Syntax ==
{{Template:SelectionCriterion:Eq syntax}}
{{Template:SelectionCriterion:Eq syntax}}
{{Template:SelectionCriterion:Ne syntax}}
{{Template:SelectionCriterion:Ge syntax}}
{{Template:SelectionCriterion:Gt syntax}}
{{Template:SelectionCriterion:Le syntax}}
{{Template:SelectionCriterion:Lt syntax}}


===Syntax terms===
===Syntax terms===
Line 45: Line 42:


<tr><th>itemFunctionEnum</th>
<tr><th>itemFunctionEnum</th>
<td>A method value (a method name literal, a method variable, a class <var>Variable</var>, 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 <var class="product">User Language</var> intrinsic (numeric, string, or <var>Unicode</var>) value.  
<td>A method value (a method name literal, a method variable, a class <var>Variable</var>, 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 <var class="product">User Language</var> intrinsic (<var>String</var>, <var>Float</var>, or <var>Unicode</var>) value. The returned type determines the type of comparison used to determine if an item satisfies the criterion. See [[#examp3|Example 3]] below.
<p>
<p>
'''Note:''' For the <var>Eq</var> and <var>Ne</var> methods only, the return value of this argument's function may also be an enumeration value, as of <var class="product">Sirius Mods</var> Version 7.8. See [[#Examples|Example 1]] below.</p>
For the <var>Eq</var> and <var>Ne</var> methods only, the return value of this argument's function may also be an enumeration value, as of <var class="product">Sirius Mods</var> Version 7.8. See [[#Examples|Example 1]] below. </p></td></tr>
</td></tr>


<tr><th>result</th>
<tr><th>result</th>
<td>An intrinsic value or expression. For a string value comparison, character case does not matter. </td></tr>
<td>An intrinsic value or expression, or an enumeration value (allowed for <var>Eq</var> and <var>Ne</var> only).</td></tr>
</table>
</table>


==Usage notes==
==Usage notes==
<ul>
<ul>
As described in [[Object variables#Virtual Constructor methods|"Virtual Constructor methods"]], <var>Eq</var> and the rest of these functions can be invoked with
<li>As described in [[Object variables#Virtual Constructor methods|"Virtual Constructor methods"]], <var>Eq</var> 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,
no method object, with an explicit class specification, or with an object variable of the class,
even if that object is <var>Null</var>:
even if that object is <var>Null</var>:
Line 83: Line 79:
[[SelectionCriterion class#xmpthis|This example]] on the [[SelectionCriterion class|"SelectionCriterion class"]] page uses <var>This</var>.
[[SelectionCriterion class#xmpthis|This example]] on the [[SelectionCriterion class|"SelectionCriterion class"]] page uses <var>This</var>.


<li>If <var class="term">itemFunctionEnum</var> is an enumeration comparisons happily handle null values and that, for example,
<li>If <var class="term">itemFunctionEnum</var> is an enumeration, null values are not a problem. But the <var>Eq</var> and <var>Ne</var> methods handle nulls differently: for example,
eq(foo, true) is different from ne(foo, false) in that the former will not match
<code>Eq(foo, true)</code> does not match an item with a null value of <code>foo</code>, but <code>Ne(foo, false)</code> does.
null values while the latter will.                                                


</ul>
</ul>
Line 109: Line 104:
<li>The following request fragment selects two items from
<li>The following request fragment selects two items from
the <tt>%fool</tt> NamedArraylist in the [[SelectionCriterion class#xmpxsub|SubsetNew example]]:
the <tt>%fool</tt> NamedArraylist in the [[SelectionCriterion class#xmpxsub|SubsetNew example]]:
<pre>
<p class="code"> ...
    ...
%sel is object selectionCriterion for object foo
    %sel is object selectionCriterion for object foo
%sel = EQ(status, 'employed')
    %sel = EQ(status, 'Employed')
%fool = %fool:subsetNew(%sel)
    %fool = %fool:subsetNew(%sel)


    for %i from 1 to %fool:count
for %i from 1 to %fool:count
      print %fool:namebynumber(%i) And -
  print %fool:namebynumber(%i) And -
            %fool:itembyNumber(%i):myprint
        %fool:itembyNumber(%i):myprint
    end for
end for
    ...
...
</pre>
</p>


The result is:
The result is:
<pre>
<p class="output">Jan Green employed (income: 55000)
    Jan Green employed (income: 55000)
Jim Red employed (income: 45000)
    Jim Red employed (income: 45000)
</p>
</pre>
<div id="examp3"></div>
<li>In the following fragment, two nearly identical <var>SubsetNew</var> calls are used.  The first uses a <var>SelectionCriterion</var> whose first argument (<code>this</code>) is a <var>String</var> valued function, and so EBCDIC comparisons are used. The second uses a <var>SelectionCriterion</var> whose first argument (<code>this</code>) is a <var>Unicode</var> valued function, and so Unicode comparisons are used.
<p class="code">%hit is namedArraylist of string len 30           
%hit = new                                       
%hit('Clapton') = 'Layla'                         
%hit('Davies') = 'All Day and All of the Night'   
%hit('Simon') = '50 Ways to Leave Your Lover'     
%hit = %hit:subsetNew(LE(this, '51'))             
%hit:print                                       
%unicHit is namedArraylist of unicode             
%unicHit = new                                   
%unicHit('Clapton') = 'Layla'                     
%unicHit('Davies') = 'All Day and All of the Night'
%unicHit('Simon') = '50 Ways to Leave Your Lover' 
%unicHit = %unicHit:subsetNew(LE(this, '51'))     
%unicHit:print
</p>
The result is:
<p class="output">1: Clapton: Layla                       
2: Davies: All Day and All of the Night 
3: Simon: 50 Ways to Leave Your Lover   
1: Simon: 50 Ways to Leave Your Lover   
</p>
In an EBCDIC comparison, letters (here, <code>L</code> and <code>A</code>) are less then numeric digits (here, <code>5</code>); in a Unicode comparison, (Latin) letters are greater than numeric digits.
</ol>
</ol>



Latest revision as of 21:34, 13 August 2012

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 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, or Unicode) value.

Syntax

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

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

%selectionCriterion = [%(SelectionCriterion For itemType):]Ge( itemFunction, - %output)

%selectionCriterion = [%(SelectionCriterion For itemType):]Gt( itemFunction, - %output)

%selectionCriterion = [%(SelectionCriterion For itemType):]Le( itemFunction, - %output)

%selectionCriterion = [%(SelectionCriterion For itemType):]Lt( itemFunction, - %output)

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 (String, Float, or Unicode) value. The returned type determines the type of comparison used to determine if an item satisfies the criterion. See Example 3 below.

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, or an enumeration value (allowed for Eq and Ne only).

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.
  • If itemFunctionEnum is an enumeration, null values are not a problem. But the Eq and Ne methods handle nulls differently: for example, Eq(foo, true) does not match an item with a null value of foo, but Ne(foo, false) does.

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 reserve return value is a Boolean True:

    %sel = eq(reserve, 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)

  3. In the following fragment, two nearly identical SubsetNew calls are used. The first uses a SelectionCriterion whose first argument (this) is a String valued function, and so EBCDIC comparisons are used. The second uses a SelectionCriterion whose first argument (this) is a Unicode valued function, and so Unicode comparisons are used.

    %hit is namedArraylist of string len 30 %hit = new %hit('Clapton') = 'Layla' %hit('Davies') = 'All Day and All of the Night' %hit('Simon') = '50 Ways to Leave Your Lover' %hit = %hit:subsetNew(LE(this, '51')) %hit:print %unicHit is namedArraylist of unicode %unicHit = new %unicHit('Clapton') = 'Layla' %unicHit('Davies') = 'All Day and All of the Night' %unicHit('Simon') = '50 Ways to Leave Your Lover' %unicHit = %unicHit:subsetNew(LE(this, '51')) %unicHit:print

    The result is:

    1: Clapton: Layla 2: Davies: All Day and All of the Night 3: Simon: 50 Ways to Leave Your Lover 1: Simon: 50 Ways to Leave Your Lover

    In an EBCDIC comparison, letters (here, L and A) are less then numeric digits (here, 5); in a Unicode comparison, (Latin) letters are greater than numeric digits.

See also