findIf: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\{\{( *)Informative( *)\|" to "{{$1Feature$2|$2Informative$2|")
m (Text replacement - "_x" to "_x")
Line 9: Line 9:
|descr= Searches for an element within array for which the code evaluates to true.
|descr= Searches for an element within array for which the code evaluates to true.
Returns the zero-based index on success or -1 if not found.<br>
Returns the zero-based index on success or -1 if not found.<br>
Code on the right side of the command is evaluated for each element of the array, processed element can be referenced in code as [[_x]].
Code on the right side of the command is evaluated for each element of the array, processed element can be referenced in code as [[Magic Variables#x|_x]].


{{Feature|Informative|[[findIf]] terminates as soon as it finds an element that fulfills the condition, thus it is more efficient than [[count]] in scenarios where you need to check for presence / absence of at least one element that evaluates to true.}}
{{Feature|Informative|[[findIf]] terminates as soon as it finds an element that fulfills the condition, thus it is more efficient than [[count]] in scenarios where you need to check for presence / absence of at least one element that evaluates to true.}}
Line 17: Line 17:
|p1= array: [[Array]] - array to search in
|p1= array: [[Array]] - array to search in


|p2= code: [[Code]] - code defining an element to find, processed element can be referenced within the code as [[_x]]
|p2= code: [[Code]] - code defining an element to find, processed element can be referenced within the code as [[Magic Variables#x|_x]]


|r1= [[Number]] - Zero-based position of the first array element for which the code evaluate to true, -1 if not found
|r1= [[Number]] - Zero-based position of the first array element for which the code evaluate to true, -1 if not found


|x1= <code>[unit1, unit2, unit3] [[findIf]] {[[not]] [[alive]] [[_x]]}; {{cc|return index of the first dead unit)}}</code>
|x1= <code>[unit1, unit2, unit3] [[findIf]] {[[not]] [[alive]] [[Magic Variables#x|_x]]}; {{cc|return index of the first dead unit)}}</code>


|x2= <code>{{cc|two ways how to 'wait for all units to be dead':}}
|x2= <code>{{cc|two ways how to 'wait for all units to be dead':}}
[[waitUntil]] { [unit1, unit2, unit3] [[findIf]] {[[alive]] [[_x]]} == -1 }; {{cc|fast, terminates as soon as it finds a living unit}}
[[waitUntil]] { [unit1, unit2, unit3] [[findIf]] {[[alive]] [[Magic Variables#x|_x]]} == -1 }; {{cc|fast, terminates as soon as it finds a living unit}}
[[waitUntil]] { {[[alive]] [[_x]]} [[count]] [unit1, unit2, unit3] == 0 }; {{cc|slow, always goes through all array elements}}</code>
[[waitUntil]] { {[[alive]] [[Magic Variables#x|_x]]} [[count]] [unit1, unit2, unit3] == 0 }; {{cc|slow, always goes through all array elements}}</code>


|seealso= [[find]] [[count]]
|seealso= [[find]] [[count]]
}}
}}

Revision as of 13:57, 25 February 2021

Hover & click on the images for description

Description

Description:
Searches for an element within array for which the code evaluates to true. Returns the zero-based index on success or -1 if not found.
Code on the right side of the command is evaluated for each element of the array, processed element can be referenced in code as _x.
findIf terminates as soon as it finds an element that fulfills the condition, thus it is more efficient than count in scenarios where you need to check for presence / absence of at least one element that evaluates to true.
Groups:
Arrays

Syntax

Syntax:
array findIf code
Parameters:
array: Array - array to search in
code: Code - code defining an element to find, processed element can be referenced within the code as _x
Return Value:
Number - Zero-based position of the first array element for which the code evaluate to true, -1 if not found

Examples

Example 1:
[unit1, unit2, unit3] findIf {not alive _x}; // return index of the first dead unit)
Example 2:
// two ways how to 'wait for all units to be dead': waitUntil { [unit1, unit2, unit3] findIf {alive _x} == -1 }; // fast, terminates as soon as it finds a living unit waitUntil { {alive _x} count [unit1, unit2, unit3] == 0 }; // slow, always goes through all array elements

Additional Information

See also:
find count

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note