Core Function CountWords

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> CountWords( <string> ) </pre> === Description === Return information about words used in a string . === Parameters === ==== char ==== An ASCII code in the range 0-255 ...")
 
m (1 revision)
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
 
<pre>
 
<pre>
CountWords( <string> )
+
CountWords( <string>, <format>, <charlist>, <outputArray> )
 
</pre>
 
</pre>
  
 
=== Description ===
 
=== Description ===
  
Return information about words used in a string .
+
Return information about words used in a string.
  
 
=== Parameters ===
 
=== Parameters ===
  
==== char ====
+
==== string ====
  
An ASCII code in the range 0-255 (e.g., 65 returns the capital letter A).
+
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 ===
 
=== Return Value ===
  
Success: Returns a string containing the ASCII representation of the given code.  
+
Success: Returns an array or an integer, depending on the format chosen.  
  
Failure: Returns a blank string.
+
Failure: Returns null.
  
 
=== Remarks ===
 
=== Remarks ===
Line 24: Line 48:
  
 
=== Example ===
 
=== Example ===
 +
 +
Basic examples
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
Line 48: 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

Latest revision as of 12:37, 14 June 2015

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