Core Function CountWords
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | CountWords( <string>, <format>, <charlist> ) | + | CountWords( <string>, <format>, <charlist>, <outputArray> ) |
</pre> | </pre> | ||
Line 15: | Line 15: | ||
==== format ==== | ==== format ==== | ||
− | Specify the return value of this function. The current supported values are: | + | Optional; Specify the return value of this function. The current supported values are: |
0 - returns the number of words found | 0 - returns the number of words found | ||
Line 22: | Line 22: | ||
2 - returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself | 2 - returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself | ||
+ | |||
+ | Default: 0 | ||
==== charlist ==== | ==== charlist ==== | ||
− | A list of additional characters which will be considered as 'word' | + | Optional; A list of additional characters which will be considered as 'word' |
+ | |||
+ | Default: null | ||
+ | |||
+ | ==== outputArray ==== | ||
+ | |||
+ | Optional; If provided this array will get the return value and the CountWords() will instead return the number of words captured. | ||
+ | |||
+ | Default: null | ||
=== Return Value === | === Return Value === | ||
Line 38: | Line 48: | ||
=== Example === | === Example === | ||
+ | |||
+ | Basic examples | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
Line 62: | Line 74: | ||
my $Str = "The quick brown fox"; | my $Str = "The quick brown fox"; | ||
printr CountWords($Str, 2); | printr CountWords($Str, 2); | ||
+ | // Prints | ||
+ | // ARRAY | ||
+ | // { | ||
+ | // [0] => The | ||
+ | // [4] => quick | ||
+ | // [10] => brown | ||
+ | // [16] => fox | ||
+ | // } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Provided an array to be used for the output | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | // Return an array containing all the words found inside the string | ||
+ | // However store that return in a variable we provide | ||
+ | my $capture = array(); | ||
+ | my $Str = "The quick brown fox"; | ||
+ | // Print the word count | ||
+ | printr CountWords($Str, 1, null, $capture ); | ||
+ | // Prints 4 | ||
+ | |||
+ | // Now print the capture variable | ||
+ | printr $capture; | ||
// Prints | // Prints | ||
// ARRAY | // ARRAY |
Revision as of 20:57, 26 July 2014
CountWords( <string>, <format>, <charlist>, <outputArray> )
Contents |
Description
Return information about words used in a string.
Parameters
string
The string to use.
format
Optional; Specify the return value of this function. The current supported values are:
0 - returns the number of words found
1 - returns an array containing all the words found inside the string
2 - returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself
Default: 0
charlist
Optional; A list of additional characters which will be considered as 'word'
Default: null
outputArray
Optional; If provided this array will get the return value and the CountWords() will instead return the number of words captured.
Default: null
Return Value
Success: Returns an array or an integer, depending on the format chosen.
Failure: Returns null.
Remarks
None
Example
Basic examples
// Display how many words are found my $Str = "The quick brown fox"; echo CountWords($Str), @NL; // Prints 4 // Return an array containing all the words found inside the string my $Str = "The quick brown fox"; printr CountWords($Str, 1); // Prints // ARRAY // { // [0] => The // [1] => quick // [2] => brown // [3] => fox // } // Return an associative array, where the key is the numeric position // of the word inside the string and the value is the actual word itself my $Str = "The quick brown fox"; printr CountWords($Str, 2); // Prints // ARRAY // { // [0] => The // [4] => quick // [10] => brown // [16] => fox // }
Provided an array to be used for the output
// Return an array containing all the words found inside the string // However store that return in a variable we provide my $capture = array(); my $Str = "The quick brown fox"; // Print the word count printr CountWords($Str, 1, null, $capture ); // Prints 4 // Now print the capture variable printr $capture; // Prints // ARRAY // { // [0] => The // [4] => quick // [10] => brown // [16] => fox // }