arrayIntersect: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\[\[[Cc]ategory:[ _]?Arma[ _]3:[ _]([^|]*)[ _]?\|.*\]\]" to "{{GameCategory|arma3|$1}}")
mNo edit summary
 
(58 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Command|Comments=
{{RV|type=command
____________________________________________________________________________________________


| arma3 |Game name=
|game1= arma3
|version1= 1.48


|1.48|Game version=
|gr1= Arrays


|gr1= Arrays |GROUP1=
|descr= Intersects two arrays returning an array of unique common elements, avoiding [[nil]] values.
Using the same array as both parameters will return unique elements of that array.
Intersects only the first dimension of said arrays.
{{Feature|informative|From Arma 3 v 2.14 the order of the intersected elements would follow 'array1' order}}


____________________________________________________________________________________________


| Intersects array1 with array2 returning array of unique common elements. Additionally, using the same array for array1 and array2 will simply return array of unique elements. Intersects only 1st dimension of an array. |DESCRIPTION=
|s1= array1 [[arrayIntersect]] array2
____________________________________________________________________________________________


| array1 '''arrayIntersect''' array2|SYNTAX=
|p1= array1: [[Array]]


|p1= array1: [[Array]] |PARAMETER1=
|p2= array2: [[Array]]


|p2= array2: [[Array]] |PARAMETER2=
|r1= [[Array]]


| [[Array]] |RETURNVALUE=
|x1= <sqf>
____________________________________________________________________________________________
_arr1 = [1,2,3,4,5,2,3,4];
 
|x1= <code>_arr1 = [1,2,3,4,5,2,3,4];
_arr2 = [4,5,6,1,2,3,5,6];
_arr2 = [4,5,6,1,2,3,5,6];
[[hint]] [[str]] (_arr1 [[arrayIntersect]] _arr2); // [4,5,1,2,3]</code> |EXAMPLE1=
hint str (_arr1 arrayIntersect _arr2); // [4,5,1,2,3]
</sqf>


|x2= <code>_arr = [1,2,3,1,2,3,1,2,3,4,5];
|x2= <sqf>
[[hint]] [[str]] (_arr [[arrayIntersect]] _arr); // [1,2,3,4,5]</code> |EXAMPLE2=
_arr = [1,2,3,1,2,3,1,2,3,4,5];
hint str (_arr arrayIntersect _arr); // [1,2,3,4,5]
</sqf>


|x3= Remove [[nil]]s:<code> [[hint]] [[str]] ([1,2,[[nil]],3] [[arrayIntersect]] [1,2,[[nil]],3]); // [1,2,3]</code> |EXAMPLE3=
|x3= Remove [[nil]]s:
<sqf>hint str ([1,2,nil,3] arrayIntersect [1,2,nil,3]); // [1,2,3]</sqf>


|x4= Also works with array elements which are [[isEqualTo|equal]]:<code> [[hint]] [[str]] ([[1],[2],[3]] [[arrayIntersect]] [[2],[3],[4]]); // [[2],[3]]</code> |EXAMPLE4=
|x4= Also works with array elements which are [[isEqualTo|equal]]:
<sqf>hint str ([[1],[2],[3]] arrayIntersect [[2],[3],[4]]); // [[2],[3]]</sqf>


|x5= This command is case sensitive:<code>["cAse"] [[arrayIntersect]] ["cAse"];{{cc|["cAse"]|true}}
|x5= This command is case-sensitive:
["cAse"] [[arrayIntersect]] ["casE"];{{cc|[]|true}}</code> |EXAMPLE5=
<sqf>
____________________________________________________________________________________________
["cAse"] arrayIntersect ["cAse"]; // ["cAse"]
["cAse"] arrayIntersect ["casE"]; // []
</sqf>


| [[append]], [[set]], [[pushBack]], [[pushBackUnique]], [[apply]], [[select]], [[resize]], [[reverse]], [[count]], [[find]], [[in]], [[forEach]], [[deleteAt]], [[deleteRange]], [[sort]], [[param]], [[params]], [[toArray]], [[toString]], [[splitString]], [[joinString]], [[BIS_fnc_arrayPushStack]], [[BIS_fnc_arrayPush]] |SEEALSO=
|seealso= [[append]] [[set]] [[pushBack]] [[pushBackUnique]] [[apply]] [[select]] [[resize]] [[reverse]] [[count]] [[find]] [[in]] [[forEach]] [[deleteAt]] [[deleteRange]] [[sort]] [[param]] [[params]] [[toArray]] [[toString]] [[splitString]] [[joinString]] [[BIS_fnc_arrayPushStack]] [[BIS_fnc_arrayPush]]
}}
 
{{Note
|user= Hypoxic125
|timestamp= 20240526165002
|text= Selectively filter out elements from _arrayA based on _arrayB.
* Designed to remove the first occurrence of each element found in _arrayB from _arrayA.
* Useful in removing elements where duplicates may exist.
 
<sqf>
private _arrayA = [1, 2, 3, 2, 4, 5, 4];
private _arrayB = [2, 2, 4];
 
{
private _index = _arrayA find _x;
if (_index != -1) then
{
_arrayA deleteAt _index;
};
} forEach _arrayB;


_arrayA; // is now [1, 3, 5, 4]
</sqf>
}}
}}
{{GameCategory|arma3|New Scripting Commands List}}
[[Category:Scripting_Commands_Arma_3]]

Latest revision as of 19:57, 26 May 2024

Hover & click on the images for description

Description

Description:
Intersects two arrays returning an array of unique common elements, avoiding nil values. Using the same array as both parameters will return unique elements of that array. Intersects only the first dimension of said arrays.
From Arma 3 v 2.14 the order of the intersected elements would follow 'array1' order
Groups:
Arrays

Syntax

Syntax:
array1 arrayIntersect array2
Parameters:
array1: Array
array2: Array
Return Value:
Array

Examples

Example 1:
_arr1 = [1,2,3,4,5,2,3,4]; _arr2 = [4,5,6,1,2,3,5,6]; hint str (_arr1 arrayIntersect _arr2); // [4,5,1,2,3]
Example 2:
_arr = [1,2,3,1,2,3,1,2,3,4,5]; hint str (_arr arrayIntersect _arr); // [1,2,3,4,5]
Example 3:
Remove nils:
hint str ([1,2,nil,3] arrayIntersect [1,2,nil,3]); // [1,2,3]
Example 4:
Also works with array elements which are equal:
hint str ([[1],[2],[3]] arrayIntersect [[2],[3],[4]]); // [[2],[3]]
Example 5:
This command is case-sensitive:
["cAse"] arrayIntersect ["cAse"]; // ["cAse"] ["cAse"] arrayIntersect ["casE"]; // []

Additional Information

See also:
append set pushBack pushBackUnique apply select resize reverse count find in forEach deleteAt deleteRange sort param params toArray toString splitString joinString BIS_fnc_arrayPushStack BIS_fnc_arrayPush

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
Hypoxic125 - c
Posted on May 26, 2024 - 16:50 (UTC)
Selectively filter out elements from _arrayA based on _arrayB.
  • Designed to remove the first occurrence of each element found in _arrayB from _arrayA.
  • Useful in removing elements where duplicates may exist.
private _arrayA = [1, 2, 3, 2, 4, 5, 4]; private _arrayB = [2, 2, 4]; { private _index = _arrayA find _x; if (_index != -1) then { _arrayA deleteAt _index; }; } forEach _arrayB; _arrayA; // is now [1, 3, 5, 4]