Core Function CountWords

From Sputnik Wiki
Jump to: navigation, search
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
// }
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox