FindPreviousItem (Arraylist function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
 
(13 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Template:Arraylist:FindPreviousItem subtitle}}
{{Template:Arraylist:FindPreviousItem subtitle}}
<var>FindPreviousItem</var> searches "backward" in the <var>Arraylist</var> to find and return the previous item that matches a specified criterion.  The criterion is supplied by the <var>[[SelectionCriterion class|SelectionCriterion]]</var> object that is a required <var>FindPreviousItem</var> parameter. If no item satisfies the criterion, the request is cancelled and an <var>[[ItemNotFound class|ItemNotFound]]</var> exception is thrown.
<var>FindPreviousItem</var> searches "backward" in the <var>Arraylist</var> to find and return the previous item that matches a specified criterion.  The criterion is supplied by the <var>[[SelectionCriterion class|SelectionCriterion]]</var> object that is a required <var>FindPreviousItem</var> parameter. If no item satisfies the criterion, the request is cancelled and an <var>[[ItemNotFound class|ItemNotFound]]</var> exception is thrown.


Line 9: Line 8:
<tr><th>%item</th>
<tr><th>%item</th>
<td>A variable of the type of the items in the <var>Arraylist</var> to contain the item that is located by the search. </td></tr>
<td>A variable of the type of the items in the <var>Arraylist</var> to contain the item that is located by the search. </td></tr>
<tr><th><var class="term">al</var></th>
 
<tr><th>al</th>
<td>An <var>Arraylist</var> object. </td></tr>
<td>An <var>Arraylist</var> object. </td></tr>
<tr><th>selectionCriterion</th><td>A <var class="term">[[SelectionCriterion class|selectionCriterion]]</var> object, which is a relational expression that is applied to an <var class="term">al</var> item value to determine whether the value satisfies the expression. The expression consists of a function, an operator, and a numeric or string value.<p>For example, <code>GT(this, -11)</code> is the criterion <code>this</code> <code>></code> <code>-11</code>, where <var>this</var> is an identity function that simply returns the item's value. The first item tested by <var>FindPreviousItem</var> that satisfies this expression is the method's return value.</p><p>The function in the criterion is a method value (a method or class member name literal, or a method variable) for a method that operates on items of the type specified on the <var class="term">al</var> declaration and that returns a numeric or string value. This is described further in [[SelectionCriterion class#Specifying a SelectionCriterion's parameters|Specifying a SelectionCriterion's parameters]]. </p></td></tr>
 
<tr><th>Start</th><td>The <var class="term">number</var> of the item <b><i>before</i></b> which to begin the search. If the <var class="term">number</var> value of <var class="term">Start</var> is 4, item 3 is the first item considered. The value of <var class="term">Start</var> may be 1 through the number of items in the collection. For a collection of three items, <var class="term">number</var> may be 1, 2, 3, or 4.<p>The default value for <var>Start</var> is the number of items in the collection plus one; to start from the last item in <var class="term">al</var>.</p><p>This is an optional, but <var>[[Methods#Named parameters|NameRequired]]</var>, argument; if you specify a value, the parameter name <var class="term">Start</var> is required.</p>
<tr><th>selectionCriterion</th>
<td>A <var>[[SelectionCriterion class|selectionCriterion]]</var> object, which is a relational expression that is applied to an <var class="term">al</var> item value to determine whether the value satisfies the expression. The expression consists of a function, an operator, and a numeric or string value.
<p>
For example, <code>GT(this, -11)</code> is the criterion <code>this</code> <code>></code> <code>-11</code>, where <var>this</var> is an identity function that simply returns the item's value. The first item tested by <var>FindPreviousItem</var> that satisfies this expression is the method's return value.</p>
<p>
The function in the criterion is a method value (a method or class member name literal, or a method variable) for a method that operates on items of the type specified on the <var class="term">al</var> declaration and that returns a numeric or string value. This is described further in [[SelectionCriterion class#Specifying a SelectionCriterion's parameters|"Specifying a SelectionCriterion's parameters"]]. </p></td></tr>
 
<tr><th><var>Start</var></th>
<td>The <var class="term">number</var> of the item <b><i>before</i></b> which to begin the search. If the <var class="term">number</var> value of <var>Start</var> is 4, item 3 is the first item considered. The value of <var>Start</var> may be 1 through the number of items in the collection. For a collection of three items, <var class="term">number</var> may be 1, 2, 3, or 4.<p>The default value for <var>Start</var> is the number of items in the collection plus one; to start from the last item in <var class="term">al</var>.</p>
<p>
This is an optional, [[Notation conventions for methods#Named parameters|name required]], argument; if you specify a value, the parameter name <var>Start</var> is required.</p>
</table>
</table>


==Usage notes==
==Usage notes==
<ul><li>If the value of <var class="term">Start</var> is not in the range of 1 through one plus the number of items in the <var>Arraylist</var>, the request is cancelled.  If the value of <var class="term">Start</var> is 1, no item can be found and the request is cancelled, but an <var>[[ItemNotFound class|ItemNotFound]]</var> exception is thrown.
<ul><li>If the value of <var>Start</var> is not in the range of 1 through one plus the number of items in the <var>Arraylist</var>, the request is cancelled.  If the value of <var class="term">Start</var> is 1, no item can be found and the request is cancelled, but an <var>[[ItemNotFound class|ItemNotFound]]</var> exception is thrown.
<li><var>FindPreviousItem</var> is available in <var class+"product">Sirius Mods</var> version 7.6 and later.
<li><var>FindPreviousItem</var> is available in <var class="product">[[Sirius Mods|Sirius Mods]]</var> Version 7.6 and later.
</ul>
</ul>


==Examples==
==Examples==
<ol><li>The following request contains two <var>FindPreviousItem</var> calls: the first, for an <var>Arraylist</var> of strings, starts checking the last item first, by default; the second call, for an <var>Arraylist</var> of float, uses a compound criterion:
The following request contains two <var>FindPreviousItem</var> calls: the first, for an <var>Arraylist</var> of strings, starts checking the last item first, by default; the second call, for an <var>Arraylist</var> of float, uses a compound criterion:
<p class="code">begin
<p class="code">begin
   %al is  arraylist of string len 10
   %al is  arraylist of string len 10
Line 28: Line 38:
               'bedford', 'boxford')
               'bedford', 'boxford')
   %al2 = list(111, 29, 0, 93, 77, -345)
   %al2 = list(111, 29, 0, 93, 77, -345)
 
   %sel is object selectioncriterion for string len 10
   %sel is object selectioncriterion for string len 10
   %sel2 is object selectioncriterion for float
   %sel2 is object selectioncriterion for float
   %sel = LT(this, 'bos')
   %sel = LT(this, 'bos')
   %sel2 = OR(GT(this, 100), LT(this, 0))
   %sel2 = OR(GT(this, 100), LT(this, 0))
 
   printtext {~} is: {%al:findPreviousItem(%sel)}
   [[PrintText statement|printText]] {~} is: {%al:findPreviousItem(%sel)}
   printtext {~} is: {%al2:findPreviousItem(%sel2, start=6)}
   printtext {~} is: {%al2:findPreviousItem(%sel2, start=6)}
end
end
Line 43: Line 53:
%al2:findPreviousItem(%sel2, start=6) is: 111
%al2:findPreviousItem(%sel2, start=6) is: 111
</p>
</p>
</ol>


==See also==
==See also==
<ul><li><var>[[FindNextItem (Arraylist function)|FindNextItem]]</var> is identical to <var>FindPreviousItem</var> except that it searches forward in the <var>Arraylist</var>.</ul>
<ul><li><var>[[FindNextItem (Arraylist function)|FindNextItem]]</var> is identical to <var>FindPreviousItem</var> except that it searches forward in the <var>Arraylist</var>.</ul>
{{Template:Arraylist:FindPreviousItem footer}}
{{Template:Arraylist:FindPreviousItem footer}}

Latest revision as of 23:23, 9 August 2012

Find matching Arraylist item (Arraylist class)

FindPreviousItem searches "backward" in the Arraylist to find and return the previous item that matches a specified criterion. The criterion is supplied by the SelectionCriterion object that is a required FindPreviousItem parameter. If no item satisfies the criterion, the request is cancelled and an ItemNotFound exception is thrown.

Syntax

%item = al:FindPreviousItem( selectionCriterion, [Start= number]) Throws ItemNotFound

Syntax terms

%item A variable of the type of the items in the Arraylist to contain the item that is located by the search.
al An Arraylist object.
selectionCriterion A selectionCriterion object, which is a relational expression that is applied to an al item value to determine whether the value satisfies the expression. The expression consists of a function, an operator, and a numeric or string value.

For example, GT(this, -11) is the criterion this > -11, where this is an identity function that simply returns the item's value. The first item tested by FindPreviousItem that satisfies this expression is the method's return value.

The function in the criterion is a method value (a method or class member name literal, or a method variable) for a method that operates on items of the type specified on the al declaration and that returns a numeric or string value. This is described further in "Specifying a SelectionCriterion's parameters".

Start The number of the item before which to begin the search. If the number value of Start is 4, item 3 is the first item considered. The value of Start may be 1 through the number of items in the collection. For a collection of three items, number may be 1, 2, 3, or 4.

The default value for Start is the number of items in the collection plus one; to start from the last item in al.

This is an optional, name required, argument; if you specify a value, the parameter name Start is required.

Usage notes

  • If the value of Start is not in the range of 1 through one plus the number of items in the Arraylist, the request is cancelled. If the value of Start is 1, no item can be found and the request is cancelled, but an ItemNotFound exception is thrown.
  • FindPreviousItem is available in Sirius Mods Version 7.6 and later.

Examples

The following request contains two FindPreviousItem calls: the first, for an Arraylist of strings, starts checking the last item first, by default; the second call, for an Arraylist of float, uses a compound criterion:

begin %al is arraylist of string len 10 %al2 is arraylist of float %al = list('boston', 'belmont', 'brighton', - 'bedford', 'boxford') %al2 = list(111, 29, 0, 93, 77, -345) %sel is object selectioncriterion for string len 10 %sel2 is object selectioncriterion for float %sel = LT(this, 'bos') %sel2 = OR(GT(this, 100), LT(this, 0)) printText {~} is: {%al:findPreviousItem(%sel)} printtext {~} is: {%al2:findPreviousItem(%sel2, start=6)} end

The result is:

%al:findPreviousItem(%sel) is: bedford %al2:findPreviousItem(%sel2, start=6) is: 111

See also

  • FindNextItem is identical to FindPreviousItem except that it searches forward in the Arraylist.