From 621ecb3d2670b7ec58ca3511eecfbd6697c11761 Mon Sep 17 00:00:00 2001 From: "[VB]AWOL" Date: Thu, 14 Nov 2013 11:36:45 -0600 Subject: [PATCH] fixed #475 --- .../compile/BIS_fnc/fn_findNestedElement.sqf | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 SQF/dayz_code/compile/BIS_fnc/fn_findNestedElement.sqf diff --git a/SQF/dayz_code/compile/BIS_fnc/fn_findNestedElement.sqf b/SQF/dayz_code/compile/BIS_fnc/fn_findNestedElement.sqf new file mode 100644 index 000000000..9c395a13b --- /dev/null +++ b/SQF/dayz_code/compile/BIS_fnc/fn_findNestedElement.sqf @@ -0,0 +1,78 @@ +scriptName "Functions\arrays\fn_findNestedElement.sqf"; +/* + File: fn_findNestedElement.sqf + Author: Joris-Jan van 't Land + + Description: + Function to find item in nested arrays and return a path to the first match. + + Example: + _array = [[1, 2], [3, 4]]; + _path = [_array, 3] call BIS_fnc_findNestedElement; //Expected result: [1, 0] + + Parameter(s): + _this select 0: Array + _this select 1: queried element (Any Value) + + Returns: + Array path to the element +*/ + +private ["_array", "_query", "_path", "_found"]; +_array = _this select 0; +_query = _this select 1; +_path = []; +_found = false; + +//Parameter checking. +if ((typeName _array) != (typeName [])) exitWith {debugLog "Log: [findNestedElement] Array (0) should be an Array!"}; + +//Find a value in an Array or one of its children. +private ["_searchArrayFunc"]; +_searchArrayFunc = +{ + private ["_array", "_query", "_find"]; + _array = _this select 0; + _query = _this select 1; + + //See if the array itself contains the queried element. + _find = _array find _query; + if (_find != -1) exitWith + { + _path = _path + [_find]; + _found = true; + }; + + //If not, search its children. + [_array, _query] call _searchArrayChildrenFunc; +}; + +//Find a value in an Array's children. +private ["_searchArrayChildrenFunc"]; +_searchArrayChildrenFunc = +{ + private ["_array", "_query"]; + _array = _this select 0; + _query = _this select 1; + + //Search all Array children for the queried element. + for "_i" from 0 to ((count _array) - 1) do + { + private ["_sub"]; + _sub = _array select _i; + + if (((typeName _sub) == (typeName [])) && !_found) then + { + _path = _path + [_i]; + [_sub, _query] call _searchArrayFunc; + }; + }; + if(count path > 0)then{ + //Nothing was found, so take a step back. + if (!_found) then {_path resize ((count _path) - 1)}; + }; +}; + +[_array, _query] call _searchArrayFunc; + +_path \ No newline at end of file