"Sputnik" help  
Sputnik Help
LastOrDefault(<array>, <queryStm>, <default>)

Description

Returns the last element in an array that satisfies a specified query statement or a default value if no matching element is found.

Parameters

<array> The array to search for the last matching element.
<queryStm> The query statement to evaluate each array element.
<default> The default value to return if no matching element is found.

Return Value

Success: Returns the last element that satisfies the query statement.
Failure: Returns the specified default value if no matching element is found.

Remarks

The FirstOrDefault function is particularly useful when searching for the last element in an array that meets specific criteria defined by the query statement. It provides the flexibility to specify a default value in case no matching element is found.

Make sure to read about the ->> and the =>> operators which are very often used in Linq statements.

Related

Example

Example of usage:

Class Person
{
    my $Name;
    my $Age;
    Function __Construct($Name, $Age)
    {
        @this->$Name = $Name;
        @this->$Age = $Age;
    }
}
my $people = array(
                new Person("Eddy", 25),
                new Person("Tom", 23),
                new Person("James", 17),
                new Person("Charles", 19),
                new Person("Mike", 27),
                new Person("Steve", 25),
                new Person("James", 42),
                );
                
printr $people->>LastOrDefault($p => $p->$Name == "James", "error");
printr $people->>LastOrDefault($p => $p->$Age == 25, "error");
printr $people->>LastOrDefault($p => $p->$Name == "Jamez", "error");
printr $people->>LastOrDefault($p => $p->$Age == 26, "error");

// PRINTS
// person Class
// (
//     [name] => James
//     [age] => 42
// )
// person Class
// (
//     [name] => Steve
//     [age] => 25
// )
// error
// error

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org