Core Function GrepKeys

From Sputnik Wiki
Jump to: navigation, search
GrepKeys( <array>, <pattern>, <flag> )

Contents

Description

Returns a new array consisting of the elements of the input arrays keys that match the given regex pattern .

Parameters

array

The array to use.

pattern

A regular expression pattern to use.

OR

An array of regular expression patterns to use.

flag

Optional; If the flag is higher than 0 the search will be inverted and everything that does not match will be returned instead of everything that does match. (Default is 0)

Return Value

Success - Returns new array with information.

Failure - Returns empty array.

Remarks

This also includes Hash values with the array values.

Example

Return everything that matches

my $array = array( "One" => "Cat", "Two222" => "Fire", "Three" => "FoX", "Four444" => "Water" );
my $lol = GrepKeys( $array, m/\d+/ );
foreach($lol as $key => $value)
{
	println("$key => $value");
}

Using array of patterns

my $array = array( "One" => "Cat", "Two222" => "Fire", "Three" => "FoX", "Four444" => "Water", "Five?" => "Dog" );
my $lol = GrepKeys( $array, array(m/\d+/, m/\?/) );
foreach($lol as $key => $value)
{
	println("$key => $value");
}

Heres an example of a GrepKeys() created by using Grep()

my $array = array( "One" => "Cat", "Two222" => "Fire", "Three" => "FoX", "Four444" => "Water" );
my $lol = _GrepKeys( $array, m/\d+/ );
foreach($lol as $key => $value)
{
	println("$key => $value");
}
 
// A custom made reverse Grep for returning matching kesy instead of matching values
Function _GrepKeys( $input, $pattern, $flags = 0 )
{
	my $keys = Grep( Keys($input), $pattern, $flags );
	my $vals = array();
	foreach ( $keys as $key )
		$vals[$key] = $input[$key];
	return $vals;
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox