Core Function Where
From Sputnik Wiki
(Difference between revisions)
(Created page with "<pre> Where( <array>, <query> ) </pre> === Description === Filters an array based on a predicate === Parameters === ==== array ==== The array to use. ==== query ==== A que...") |
|||
(3 intermediate revisions by one user not shown) | |||
Line 5: | Line 5: | ||
=== Description === | === Description === | ||
− | Filters an array based on a | + | Filters an array based on a query and returns all matching. |
=== Parameters === | === Parameters === | ||
Line 57: | Line 57: | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
+ | // Define a Person class | ||
+ | Class Person | ||
+ | { | ||
+ | my $Name; | ||
+ | my $Age; | ||
+ | Function __Construct($Name, $Age) | ||
+ | { | ||
+ | $this->$Name = $Name; | ||
+ | $this->$Age = $Age; | ||
+ | } | ||
+ | }; | ||
// Create an array of them | // Create an array of them | ||
$people = array( // Each one has a different name and age | $people = array( // Each one has a different name and age |
Latest revision as of 23:35, 21 June 2015
Where( <array>, <query> )
Contents |
Description
Filters an array based on a query and returns all matching.
Parameters
array
The array to use.
query
A query to test each element for a condition
Return Value
Success: An Array that contains elements from the input sequence that satisfy the condition
Failure: Returns null.
Remarks
None.
Example
Return all items from an array that are below 3 in length
$a = array("One", "Two", "Three", "Four"); foreach($a->>Where($v => StrLen($v) < 4) as $i) { say $i; } // Prints // One // Two
Return all items from an array that are below 3 in length and starts with the letter T
$a = array("One", "Two", "Three", "Four"); foreach($a->>Where($v => StrLen($v) < 4 && StartsWith($v, 'T')) as $i) { say $i; } // Prints // Two
Return all items from an array that contains class objects and filter it so only ones with a certain value range are returned
// Define a Person class Class Person { my $Name; my $Age; Function __Construct($Name, $Age) { $this->$Name = $Name; $this->$Age = $Age; } }; // Create an array of them $people = array( // Each one has a different name and age new Person("Tom", 23), new Person("James", 17), new Person("Charles", 19), new Person("Mike", 27), new Person("Steve", 25) ); // Using Where() we will loop through and find the ones we want // Lets find all the people who are under 25 foreach($people->>Where($p => $p->$Age < 25) as $person) { printf("Name %s | Age %d\n", $person->$Name, $person->$Age); } // Prints // Name Tom | Age 23 // Name James | Age 17 // Name Charles | Age 19
Same as above but with an extra check
// Define a Person class Class Person { my $Name; my $Age; Function __Construct($Name, $Age) { $this->$Name = $Name; $this->$Age = $Age; } }; // Create an array of them $people = array( // Each one has a different name and age new Person("Tom", 23), new Person("James", 17), new Person("Charles", 19), new Person("Mike", 27), new Person("Steve", 25) ); // Using Where() we will loop through and find the ones we want // Lets find all the people who are under 25 but not younger than 18 foreach($people->>Where($p => $p->$Age < 25 && $p->$Age > 18) as $person) { printf("Name %s | Age %d\n", $person->$Name, $person->$Age); } // Prints // Name Tom | Age 23 // Name Charles | Age 19
Same as above but with some changes
// Define a Person class Class Person { my $Name; my $Age; Function __Construct($Name, $Age) { $this->$Name = $Name; $this->$Age = $Age; } }; // Create an array of them $people = array( // Each one has a different name and age new Person("Tom", 23), new Person("James", 17), new Person("Charles", 19), new Person("Toby", 14), new Person("Mike", 27), new Person("Steve", 25) ); // Using Where() we will loop through and find the ones we want // Lets find all the people who are under 25 but not younger than 18 foreach($people->>Where($p => $p->$Age < 25 && StartsWith($p->$Name, 'T')) as $person) { printf("Name %s | Age %d\n", $person->$Name, $person->$Age); } // Prints // Name Tom | Age 23 // Name Toby | Age 14