Core Function Where
From Sputnik Wiki
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