Jump to content
Sign in to follow this  
Avaz

Searching Array

Recommended Posts

Hi, im basically trying to figure how to search through an array, its pretty simple I think but Im fairly new to scripting and cant really figure it out.

 

I have an array which contains a classnames/prices for weapons.

 



base_weapons = [
  ["arifle_MXC_F",100],
  ["arifle_MX_F",200],
  ["arifle_MX_GL_F",300],
  ["arifle_MX_SW_F",400], 
  ["arifle_MXM_F",500],
  ["LMG_Mk200_F",600],
  ["srifle_GM6_F",700]
];
 


 

I then have another script where I want to search if any of the items in the _items array are in the base_weapons array (and the price in the base_weapons is irrelevent here).

 



_items  = ["arifle_MXC_F","arifle_MX_F","srifle_GM6_F"];
 
{
if (_x in base_weapons) then {whatever};
} forEach _items
 


 

This is where the issue is I think, I know that I need to only search the first classname section of the elements in the base_weapons array, so I was thinking 

something like (_x in (base_weapons select 0)), but if I understand correctly this would only search in ["arifle_MXC_F",100] so it wouldnt work, but I want to check the classnames in _items against all the classnames in base_weapons. Maybe trying it in the wrong way im not sure.

 

Sorry if this isn't explained well, and thanks in advance.

 

Share this post


Link to post
Share on other sites

Two options:

1) fn_transformArray.sqf

This will transform arrays like e.g.

[
    [string, number],
    [string, number],
    [string, number],
    ...
]

into

[
    [string, string, string, ...],
    [number, number, number, ...]
]

That gives you the advantage of using the "find" and "in" commands on either subarray which makes searches by far faster than with BIS_fnc_findNestedArray, plus you can't use "in" with BIS_fnc_findNestedArray. (Plus, I consider it more handy than BIS_fnc_findNestedElement which is why I created it in the first place.)

 

2) BIS_fnc_findNestedElement
This has the charme that you don't have to change your array at all. In your case, this:
[base_weapons, "arifle_MX_GL_F"] call BIS_fnc_findNestedArray

will return [2, 0] where 2 is the position of the array where "arifle_MX_GL_F" is in and 0 is the position of that string inside the subarray.

  • Like 2

Share this post


Link to post
Share on other sites

Thanks very much, ill try this when I get chance later  :)

Share this post


Link to post
Share on other sites

Edit: Nevermind, got it working perfectly and just had a simple error as I made a mistake.

 

I was wondering however, does using 'in' not work as I also tried it and got the error : Error if: Type Number, expected Bool.

_items  = ["arifle_MXC_F","arifle_MX_F","srifle_GM6_F"];

base_weapons_transformed = [base_weapons,2,false] call fn_transformArray;

{
if (_x in base_weapons_transformed select 0) then {whatever};
} forEach _items;

 Thanks again for sharing anyway, it works perfectly  :D

Share this post


Link to post
Share on other sites

Edit: Nevermind, got it working perfectly and just had a simple error as I made a mistake.

 

I was wondering however, does using 'in' not work as I also tried it and got the error : Error if: Type Number, expected Bool.

_items  = ["arifle_MXC_F","arifle_MX_F","srifle_GM6_F"];

base_weapons_transformed = [base_weapons,2,false] call fn_transformArray;

{
if (_x in base_weapons_transformed select 0) then {whatever};
} forEach _items;

 

If I execute your code, I get a generic error due to a parantheses issue:

 

if (_x in (base_weapons_transformed select 0)) then...

 

But since your error message says "Error if:...", I guess this should fix it anyways.

 

Thanks again for sharing anyway, it works perfectly :D

I'm glad you like it. :)

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  

×