LoonyWarrior 10 Posted May 30, 2013 hi is there a method for selecting max value from the array? Share this post Link to post Share on other sites
Larrow 2823 Posted May 30, 2013 /************************************************************ Find Greatest Number By Andrew Barron Parameters: [number set] Returns: number Returns the highest number out of the passed set. Example: [1,5,10] call BIS_fnc_greatestNum returns 10 ************************************************************/ Share this post Link to post Share on other sites
dmarkwick 261 Posted May 30, 2013 Header for the fn_sortBy.sqf function that BIS supply: /* Author: Jiri Wainar (Warka) Description: Sorts an array according to given algorithm. Parameter(s): _this select 0: any unsorted array (Array) - array can contain any types (object/numbers/strings ..) _this select 1: sorted algorithm (Code) [optional: default {_x}] - code needs to return a scalar - variable _x refers to array item _this select 2: sort direction (String) [optional: default "ASCEND"] "ASCEND": sorts array in ascending direction (from lowest value to highest) "DESCEND": sorts array in descending direction _this select 3: filter (Code) [optional: default {true}] - code that needs to evaluate true for the array item to be sorted, otherwise item is removed Returns: Array Examples: _sortedNumbers = [[1,-80,0,480,15,-40],{_x},"ASCEND"] call BIS_fnc_sortBy; _closestHelicopters = [[_heli1,_heli2,_heli3],{player distance _x},"ASCEND"] call BIS_fnc_sortBy; _furtherstEnemy = [[_enemy1,_enemy2,_enemy3],{player distance _x},"DESCEND",{canMove _x}] call BIS_fnc_sortBy; */ Bolded is of particular interest to you. (Although I suggest you use "DESCEND" and select the first element :)) Share this post Link to post Share on other sites
LoonyWarrior 10 Posted May 30, 2013 BIS_fnc_greatestNum super ! ...thank u Share this post Link to post Share on other sites
killzone_kid 1332 Posted May 30, 2013 /************************************************************ Find Greatest Number By Andrew Barron Parameters: [number set] Returns: number Returns the highest number out of the passed set. Example: [1,5,10] call BIS_fnc_greatestNum returns 10 ************************************************************/ Just tested this vs plain max command, what a waste of time tbh. _t = diag_tickTime; for "_i" from 1 to 100000 do { _k = 1 max 5 max 10; }; hint str (diag_tickTime - _t); //0.32 seconds _t = diag_tickTime; for "_i" from 1 to 100000 do { _k = [1, 5, 10] call BIS_fnc_greatestNum; }; hint str (diag_tickTime - _t); //4.57 seconds Share this post Link to post Share on other sites
Larrow 2823 Posted May 30, 2013 (edited) Aye was easy to point to though. Also sod writing out that max line with a 100 index :P Strange enough BIS_fnc_greatestNum in its raw form _nums = [1,5,10]; _t = diag_tickTime; for "_i" from 1 to 100000 do { _highest = -1e9; { if(_x > _highest) then { _highest = _x; }; } foreach _nums; }; hint str (diag_tickTime - _t); //1.5 around abouts nums = [1,5,10]; highest = -1e9; t = diag_tickTime; for "_i" from 1 to 100000 do { for "_n" from 0 to ((count nums)-1) do { highest = highest max (nums select _n); }; }; hint str (diag_tickTime - t); //1.3 around abouts nums = [1,5,10]; highest = -1e9; t = diag_tickTime; for "_i" from 1 to 100000 do { { highest = highest max _x; }forEach nums; }; hint str (diag_tickTime - t); //1.05 around abouts Edited May 30, 2013 by Larrow Share this post Link to post Share on other sites
LoonyWarrior 10 Posted May 30, 2013 Just tested this vs plain max command, what a waste of time tbh. nice... thx... Share this post Link to post Share on other sites
killzone_kid 1332 Posted May 30, 2013 Aye was easy to point to though. Also sod writing out that max line with a 100 index :PStrange enough BIS_fnc_greatestNum in its raw form _nums = [1,5,10]; _t = diag_tickTime; for "_i" from 1 to 100000 do { _highest = -1e9; { if(_x > _highest) then { _highest = _x; }; } foreach _nums; }; hint str (diag_tickTime - _t); //1.5 around abouts Yeah noticed that, not great. Pretty sure if you need to deal with long arrays there could be another way. In most cases though custom approach is the best. Share this post Link to post Share on other sites
LoonyWarrior 10 Posted May 30, 2013 nums = [1,5,10]; highest = -1e9; t = diag_tickTime; for "_i" from 1 to 100000 do { { highest = highest max _x; }forEach nums; }; hint str (diag_tickTime - t); //1.05 around abouts i just found the winner... :D Share this post Link to post Share on other sites
killzone_kid 1332 Posted May 31, 2013 (edited) Also sod writing out that max line with a 100 index :P This got me thinking, if only Arma had support for regular expressions... But C++ has! So I wrote 2 extensions regex_match and regex_replace. And this is how it could be done with lightning speed _array = [10,50,45,33]; _max = call compile ( "regex_replace" callExtension "/,/ max /" + ( "regex_replace" callExtension "/\[|\]//" + str _array ) ); hint str _max; I'm not kidding about lightning speed, matching a word in a string is 100x faster with RegExp than trying to solve the same problem with sqf. Gonna release both dlls soon. Edited May 31, 2013 by Killzone_Kid Share this post Link to post Share on other sites