WordNumberIn and WordNumberOf (String functions): Difference between revisions
mNo edit summary |
No edit summary |
||
(6 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{Template:String:WordNumberIn and WordNumberOf subtitle}} | {{Template:String:WordNumberIn and WordNumberOf subtitle}} | ||
The <var>WordNumberIn</var> and <var>WordNumberOf</var> functions return the word number of the first occurrence of one word (the "needle") within a list of words ("the haystack"). The search respects character case. | |||
The | The only difference between the two methods is that for <var>WordNumberIn</var>, the method object string is the "needle" and the first argument is the "haystack", whereas for <var>WordNumberOf</var>, the first argument is the "needle" and the method object string is the "haystack". Which method is more convenient to use will be application dependent. | ||
<p></p> | <p></p> | ||
Line 11: | Line 12: | ||
===Syntax terms=== | ===Syntax terms=== | ||
<table class="syntaxTable"> | <table class="syntaxTable"> | ||
<tr><th>%number</th><td> A variable to receive the number of the first occurrence of the | <tr><th nowrap>%number</th><td> A variable to receive the number of the first occurrence of the "needle" word in the "haystack" list of words. If the word "needle" is not one of the words in "haystack", <var class="term">%number</var> is set to 0. | ||
</td></tr> | </td></tr> | ||
<tr><th>string</th> | <tr><th>string</th> | ||
<td>The method object string | <td>The method object string; either: | ||
</td></tr> | <ul><li>the search target "needle," when called as <var>WordNumberIn</var> | ||
<li>the list of words "haystack," when called as <var>WordNumberOf</var></ul></td></tr> | |||
<tr><th>haystack</th> | <tr><th>haystack</th> | ||
<td> | <td>In <var>WordNumberIn</var>, the list of words within which the | ||
<var class="term">string</var> method object (the "needle") is searched for.</td></tr> | |||
<tr><th>needle</th> | <tr><th>needle</th> | ||
<td> | <td>In <var>WordNumberOf</var>, the word to be searched for within the <var class="term">string</var> method object list of words (the "haystack").</td></tr> | ||
</td></tr> | |||
<tr><th><var>Spaces</var></th> | <tr><th><var>Spaces</var></th> | ||
<td>A set of "whitespace" characters, that is, characters that separate words. The default is the blank character. If the null string is provided, the blank character is used as the whitespace character. | <td>A set of "whitespace" characters, that is, characters that separate words. The default is the blank character. If the null string is provided, the blank character is used as the whitespace character. | ||
</td></tr> | </td></tr> | ||
</table> | </table> | ||
==Usage notes== | |||
<ul> | |||
<li><var>WordNumberIn</var> is the OO version of <var>[[$Lstr_Windex]]</var>. | |||
</ul> | |||
==Examples== | ==Examples== | ||
The following fragment: | <ol> | ||
<li>The following fragment: | |||
<p class="code">PrintText {~='aa bb cc':WordNumberOf('bb')} | <p class="code">PrintText {~='aa bb cc':WordNumberOf('bb')} | ||
PrintText {~='bb':WordNumberIn('aa bb cc')} | PrintText {~='bb':WordNumberIn('aa bb cc')} | ||
</p> | </p> | ||
produces | produces this result: | ||
<p class="output">'aa bb cc':WordNumberOf('bb')=2 | <p class="output">'aa bb cc':WordNumberOf('bb')=2 | ||
'bb':WordNumberIn('aa bb cc')=2 | 'bb':WordNumberIn('aa bb cc')=2 | ||
</p> | </p> | ||
You can use <var>WordNumberIn</var> and <var>[[Word (String function)|Word]]</var> as | |||
<li>You can use <var>WordNumberIn</var> and <var>[[Word (String function)|Word]]</var> as | |||
"quick and dirty" <var>[[NamedArraylist class|NamedArraylists]]</var>: | "quick and dirty" <var>[[NamedArraylist class|NamedArraylists]]</var>: | ||
<p class="code">%x = 'PA' | <p class="code">%x = 'PA' | ||
Line 46: | Line 54: | ||
However, as the number of words grows large, repeated use of <var>WordNumberIn</var> | However, as the number of words grows large, repeated use of <var>WordNumberIn</var> | ||
is less efficient than using a <var>NamedArrayList</var>. | is less efficient than using a <var>NamedArrayList</var>. | ||
</ol> | |||
==See also== | ==See also== | ||
<ul> | <ul> | ||
<li>[[List of StringTokenizer methods]] | <li>[[List of StringTokenizer methods]] | ||
<li><var> | <li><var>[[Word (String function)|Word]]</var>, which selects a word from a list of words | ||
<li><var>[[Words (String function)|Words]]</var>, which produces a word sublist from a list of words | |||
<li><var>[[WordCount (String function)|WordCount]]</var>, which counts the words in a list | |||
</ul> | </ul> | ||
{{Template:String:WordNumberIn footer}} | {{Template:String:WordNumberIn footer}} |
Latest revision as of 23:04, 3 September 2015
The word number of a word within a list of words (String class)
[Introduced in Sirius Mods 7.9]
The WordNumberIn and WordNumberOf functions return the word number of the first occurrence of one word (the "needle") within a list of words ("the haystack"). The search respects character case.
The only difference between the two methods is that for WordNumberIn, the method object string is the "needle" and the first argument is the "haystack", whereas for WordNumberOf, the first argument is the "needle" and the method object string is the "haystack". Which method is more convenient to use will be application dependent.
Note that processing a string of words may be better accomplished with the StringTokenizer class.
Syntax
%number = string:WordNumberIn( haystack, [Spaces= string])
%number = string:WordNumberOf( needle, [Spaces= string])
Syntax terms
%number | A variable to receive the number of the first occurrence of the "needle" word in the "haystack" list of words. If the word "needle" is not one of the words in "haystack", %number is set to 0. |
---|---|
string | The method object string; either:
|
haystack | In WordNumberIn, the list of words within which the string method object (the "needle") is searched for. |
needle | In WordNumberOf, the word to be searched for within the string method object list of words (the "haystack"). |
Spaces | A set of "whitespace" characters, that is, characters that separate words. The default is the blank character. If the null string is provided, the blank character is used as the whitespace character. |
Usage notes
- WordNumberIn is the OO version of $Lstr_Windex.
Examples
- The following fragment:
PrintText {~='aa bb cc':WordNumberOf('bb')} PrintText {~='bb':WordNumberIn('aa bb cc')}
produces this result:
'aa bb cc':WordNumberOf('bb')=2 'bb':WordNumberIn('aa bb cc')=2
- You can use WordNumberIn and Word as
"quick and dirty" NamedArraylists:
%x = 'PA' %w = %x:WordNumberIn - ('MA PA MN') %s = ('Massachusetts Pennsylvania Minnesota'):Word(%w) Print 'Full state name:' And %s
However, as the number of words grows large, repeated use of WordNumberIn is less efficient than using a NamedArrayList.
See also
- List of StringTokenizer methods
- Word, which selects a word from a list of words
- Words, which produces a word sublist from a list of words
- WordCount, which counts the words in a list