Core Function IndexOfValue

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
(Example)
Line 122: Line 122:
  
 
$pos = IndexOfValue($arr, 'purple');
 
$pos = IndexOfValue($arr, 'purple');
 +
say "Positon is: $pos"; // 1
 +
</syntaxhighlight>
 +
 +
Example of using Regexp as the needle
 +
 +
<syntaxhighlight lang="sputnik">
 +
my $arr = array("One", "One1", "One", "One");
 +
 +
$pos = IndexOfValue($arr, m/\d+/);
 
say "Positon is: $pos"; // 1
 
say "Positon is: $pos"; // 1
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Revision as of 06:38, 31 August 2013

IndexOfValue( <array>, <needle>, <strict>, <ignoreCase> )

Contents

Description

Checks if a value exists in an array and returns its index value

Parameters

array

The array to use.

needle

The searched value.

Note:

If needle is a string, the comparison is done in a case-sensitive manner.

(Unless the flag is set to ignore case)

You can set the needle to a Regex such as "m/\d+/" (without quotes) to search for Regexp patterns.

strict

Optional; If true the the array value and the needle must be same TYPE such as both Int32s.

Note - If "strict" is true when the needle is a Regex then it will require all the values to be STRING or else it will not match.

ignoreCase

Optional; Flag to choose if the search will be case sensitive or case insensitive

True: Ignore case and match with case insensitivity

False: Case must be an exact match

Default: false

Return Value

Success: Returns integer index if the value was located in the array section or a string of the index key if the value was found in the dictionary section.

Failure: Returns null (It has to return null since it is the only way to make sure that even every possible key match was invalid).

Remarks

None.

Example

Example of checking for a string in the array and returning its position

my $arr = array("One", "Two", "Three");
 
// Case sensitive
my $pos = IndexOfValue($arr, "Two");
say "Positon is: $pos"; // 1
 
my $pos = IndexOfValue($arr, "TwO");
say "Positon is: $pos"; // null
 
// Case insensitive
my $pos = IndexOfValue($arr, "TwO", false, true);
say "Positon is: $pos"; // 1

Example of checking for a value in the array and returning its position

my $arr = array("One", "Two", "100", "Three");
 
// Non strict
my $pos = IndexOfValue($arr, 100);
say "Positon is: $pos"; // 2
 
// Strict
my $pos = IndexOfValue($arr, 100, true);
say "Positon is: $pos"; // null


my $arr = array("One", "Two", (double)100, "Three");
 
// Non strict
my $pos = IndexOfValue($arr, 100.0);
say "Positon is: $pos"; // 2
 
// Strict
my $pos = IndexOfValue($arr, 100.0, true);
say "Positon is: $pos"; // null (because 100 is an Integer and 100.0 is a double)


my $arr = array("One", "Two", (double)100, "Three");
 
// Non strict
my $pos = IndexOfValue($arr, 100.0);
say "Positon is: $pos"; // 2
 
// Strict
my $pos = IndexOfValue($arr, 100.0, true);
say "Positon is: $pos"; // 2

Example of when the value is found to have a key that is a string and not numeric

my $arr = array('One' => 'blue', 'Two' => 'red', 'Three' => 'green', 'Four' => 'red', 'pink', 'purple');
 
$pos = IndexOfValue($arr, 'blue');
say "Positon is: $pos"; // One
 
$pos = IndexOfValue($arr, 'green');
say "Positon is: $pos"; // Three
 
$pos = IndexOfValue($arr, 'purple');
say "Positon is: $pos"; // 1

Example of using Regexp as the needle

my $arr = array("One", "One1", "One", "One");
 
$pos = IndexOfValue($arr, m/\d+/);
say "Positon is: $pos"; // 1
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox