Core Function Grep
From Sputnik Wiki
				
				
				(Difference between revisions)
				
																
				
				
								
				 (→Example)  | 
		m (1 revision)  | 
		||
| (9 intermediate revisions by one user not shown) | |||
| Line 1: | Line 1: | ||
<pre>  | <pre>  | ||
| − | Grep( <  | + | Grep( <array>, <pattern>, <flag> )  | 
</pre>  | </pre>  | ||
=== Description ===  | === Description ===  | ||
| − | Returns a new array consisting of the elements of the input   | + | Returns a new array consisting of the elements of the input arrays values that match the given regex pattern.  | 
=== Parameters ===  | === Parameters ===  | ||
| + | |||
| + | ==== array ====  | ||
| + | |||
| + | The array to use.  | ||
==== pattern ====  | ==== pattern ====  | ||
| Line 13: | Line 17: | ||
A regular expression pattern to use.  | A regular expression pattern to use.  | ||
| − | + | OR  | |
| − | + | An array of regular expression patterns to use.  | |
==== flag ====  | ==== flag ====  | ||
| Line 23: | Line 27: | ||
=== Return Value ===  | === Return Value ===  | ||
| − | Success - Returns   | + | Success - Returns new array with information.  | 
| − | Failure - Returns   | + | Failure - Returns empty array.  | 
=== Remarks ===  | === Remarks ===  | ||
| Line 37: | Line 41: | ||
<syntaxhighlight lang="sputnik">  | <syntaxhighlight lang="sputnik">  | ||
my $array = array( "Test123", "moo", "cat", "dog555", "meows"  );  | my $array = array( "Test123", "moo", "cat", "dog555", "meows"  );  | ||
| − | my $lol = Grep( m/\d+/  | + | my $lol = Grep( $array, m/\d+/ );  | 
foreach($lol as $c)  | foreach($lol as $c)  | ||
{  | {  | ||
| Line 48: | Line 52: | ||
<syntaxhighlight lang="sputnik">  | <syntaxhighlight lang="sputnik">  | ||
my $array = array( "Test123", "moo", "cat", "dog555", "meows"  );  | my $array = array( "Test123", "moo", "cat", "dog555", "meows"  );  | ||
| − | my $lol = Grep( m/\d+/, $array,   | + | my $lol = Grep( $array, m/\d+/, 1 );  | 
| + | foreach($lol as $c)  | ||
| + | {  | ||
| + | 	println("Value: $c");  | ||
| + | }  | ||
| + | </syntaxhighlight>  | ||
| + | |||
| + | Using an array of patterns  | ||
| + | |||
| + | <syntaxhighlight lang="sputnik">  | ||
| + | my $array = array( "Test123", "moo", "cat", "dog555", "meows", "hehe?s"  );  | ||
| + | my $lol = Grep( $array, array(m/\d+/, m/\?/) );  | ||
foreach($lol as $c)  | foreach($lol as $c)  | ||
{  | {  | ||
| Line 59: | Line 74: | ||
<syntaxhighlight lang="sputnik">  | <syntaxhighlight lang="sputnik">  | ||
my $array = array( "One" => "Cat", "Two222" => "Fire", "Three" => "FoX", "Four444" => "Water" );  | my $array = array( "One" => "Cat", "Two222" => "Fire", "Three" => "FoX", "Four444" => "Water" );  | ||
| − | my $lol =   | + | my $lol = _GrepKeys( $array, m/\d+/ );  | 
foreach($lol as $key <=> $value)  | foreach($lol as $key <=> $value)  | ||
{  | {  | ||
| Line 65: | Line 80: | ||
}  | }  | ||
| − | Function   | + | // A custom made reverse Grep for returning matching kesy instead of matching values  | 
| + | Function _GrepKeys( $input, $pattern, $flags = 0 )  | ||
{  | {  | ||
| − | 	my $keys = Grep(   | + | 	my $keys = Grep( Keys($input), $pattern, $flags );  | 
	my $vals = array();  | 	my $vals = array();  | ||
	foreach ( $keys as $key )  | 	foreach ( $keys as $key )  | ||
| − | |||
		$vals[$key] = $input[$key];  | 		$vals[$key] = $input[$key];  | ||
| − | |||
	return $vals;  | 	return $vals;  | ||
}  | }  | ||
Latest revision as of 12:37, 14 June 2015
Grep( <array>, <pattern>, <flag> )
Contents | 
Description
Returns a new array consisting of the elements of the input arrays values 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( "Test123", "moo", "cat", "dog555", "meows" ); my $lol = Grep( $array, m/\d+/ ); foreach($lol as $c) { println("Value: $c"); }
Return everything that does not match (by setting the flag to 1)
my $array = array( "Test123", "moo", "cat", "dog555", "meows" ); my $lol = Grep( $array, m/\d+/, 1 ); foreach($lol as $c) { println("Value: $c"); }
Using an array of patterns
my $array = array( "Test123", "moo", "cat", "dog555", "meows", "hehe?s" ); my $lol = Grep( $array, array(m/\d+/, m/\?/) ); foreach($lol as $c) { println("Value: $c"); }
In this example it returns all KEYS that match in the Hash(Dictionary) instead of the values
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; }