Jump to content
Sign in to follow this  
kovvalsky

[Array search function] Array(strings) in Array(strings)

Recommended Posts

Hi, i try to compare two arrays like this:

_ArraySt = ["a","b","c"];
_ArraySrch = ["c","d"];

_Result =_ArraySt in _ArraySrch;

//_Result is false...

then i try this but don't work..

_ArraySt = ["a","b","c"];
_ArraySrch = ["c","d"];

{
  _Result = _x in _ArraySrch;
  if (_Result) exitWith {};  //KK thanks

}forEach _ArraySt;

//still return false

How I can search on an array (containing strings) another array with strings inside too?.

thanks!

(SOLVED)

/* 
------------------------------------------------------------------------------
DESC: Returns true if search string inside array exist
------------------------------------------------------------------------------
PARAMS:
0: [ARRAY] Content array
1: [ARRAY] Search array
or
1: [sTRING] Search string
------------------------------------------------------------------------------
RETURN:
[bOOLEAN] true or false
-------------------------------------------------------------------------------	
EXAMPLE:
_Result = [["a","b","c"],["c","d"]] call your_FunctionName;
// _Result is true

_Result = [["a","b","c"],["C","D"]] call your_FunctionName;
// _Result is false

_Result = [["a","b","c"],"a"] call your_FunctionName;  
// _Result is true
------------------------------------------------------------------------------
*/

private["_Contenido","_Busqueda","_Resultado"];

_Contenido = [_this,0,[],[[]]] call BIS_fnc_param;
_Busqueda =  [_this,1,"",["",[]]] call BIS_fnc_param;
_Resultado = false;

if ((typeName _Busqueda) == "STRING") then {_Busqueda = [_Busqueda];};
if ((typeName _Contenido) == "ARRAY")then {
{
	_Resultado = _x in _Busqueda;
	if (_Resultado) exitWith {};
}forEach _Contenido;
} else {
_Resultado = _Contenido in _Busqueda;
};
_Resultado;

Edited by KoVValsky

Share this post


Link to post
Share on other sites

Sorry to steal the spotlight, but I'm just having so much fun iterating through arrays!

/*
USAGE: _result = [P1, P2] call DREAD_fnc_searchArrayForElements;
	P1: ARRAY - Array containing elements to find.
	P2: ARRAY - Array containing elements to search for.
RETURNS: An array containing arrays consisting of the indexes the
input elements were found on.

EXAMPLE:
	_search = ["a", "b", "c", "d", "a", "b", "c", "d"];
	_find   = ["a", "d"];
	_result = [_search, _find] call DREAD_fnc_searchArrayForElements;
RETURNS: [ [0, 4] , [3, 7] ];

NOTE: Function will not stop after finding the first match.
	  Function will find all instances of matching elements.
NOTE: If an element is not found, result will be "[]".
*/

DREAD_fnc_searchArrayForElements =
{
private ["_search", "_find", "_result", "_testedElement", "_curElement"];
_search = [_this, 0, [], [[]]] call BIS_fnc_param;
_find = [_this, 1, [], [[]]] call BIS_fnc_param;
_result = [];
{
	_testedElement = _x;
	_curElement = [];
	{
		if (_testedElement == _x) then
		{
			_curElement pushBack _forEachIndex;
		};
	}forEach _search;
	_result pushBack _curElement;
}forEach _find;

_result	
};

Minimally tested, pretty much only tested with strings. Theoretically can process arrays of any size, though, again, minimally tested. Hope this helps.

Edited by DreadedEntity

Share this post


Link to post
Share on other sites
Clarify a little for me, so are you trying to see if any elements in _arraySrch are contained in _arraySt?

yes :)

fixed :)

DreadedEntity thanks, old school solution. :p

Final function:

private["_Contenido","_Busqueda","_Resultado"];
_Contenido = [_this,0,[],[[]]] call BIS_fnc_param;
_Busqueda =  [_this,1,"",["",[]]] call BIS_fnc_param;
_Resultado = false;

if ((typeName _Busqueda) == "STRING") then {_Busqueda = [_Busqueda];};
if ((typeName _Contenido) == "ARRAY")then {
{
	_Resultado = _x in _Busqueda;
	if (_Resultado) exitWith {};
}forEach _Contenido;
} else {
_Resultado = _Contenido in _Busqueda;
};
_Resultado;

Edited by KoVValsky

Share this post


Link to post
Share on other sites

//still return false

[/php]

works for me now

[color="#FF8040"][color="#1874CD"]_ArraySt[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"a"[/color][color="#8B3E2F"][b],[/b][/color][color="#7A7A7A"]"b"[/color][color="#8B3E2F"][b],[/b][/color][color="#7A7A7A"]"c"[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_ArraySrch[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"c"[/color][color="#8B3E2F"][b],[/b][/color][color="#7A7A7A"]"d"[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]

[color="#8B3E2F"][b]{[/b][/color]
  [color="#1874CD"]_Result[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#000000"]_x[/color] [color="#191970"][b]in[/b][/color] [color="#1874CD"]_ArraySrch[/color][color="#8B3E2F"][b];[/b][/color]
  [color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_Result[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#191970"][b]hint[/b][/color] [color="#7A7A7A"]"match found"[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color] 
[color="#8B3E2F"][b]}[/b][/color] [color="#191970"][b]forEach[/b][/color] [color="#1874CD"]_ArraySt[/color][color="#8B3E2F"][b];[/b][/color][/color]

Share this post


Link to post
Share on other sites

THANKs All!

Final function:

private["_Contenido","_Busqueda","_Resultado"];
_Contenido = [_this,0,[],[[]]] call BIS_fnc_param;
_Busqueda =  [_this,1,"",["",[]]] call BIS_fnc_param;
_Resultado = false;

if ((typeName _Busqueda) == "STRING") then {_Busqueda = [_Busqueda];};
if ((typeName _Contenido) == "ARRAY")then {
{
	_Resultado = _x in _Busqueda;
	if (_Resultado) exitWith {};
}forEach _Contenido;
} else {
_Resultado = _Contenido in _Busqueda;
};
_Resultado;

Share this post


Link to post
Share on other sites
old school solution old school solution old school solution

hurts my feelings :p

Anyway the problem with using in is that you can only find if an element is in the array and the problem with using count is it only tells you how many matches there are. That code I posted will tell you, if, how many, and their position within the array to search. :coop:

Share this post


Link to post
Share on other sites
hurts my feelings :p

jejej sorry!! not my intention, i love old school

I really like your function, I appreciate the contribution

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×