Jump to content
LoOni3r

get a a certain value in the array

Recommended Posts

hi Guys,

 

can arma do something similar?

 

Example PHP

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

 

MyTest

_array = [["AAA",111],["BBB",222],["CCC",333]]; 
getArray (_array >> "AAA")

I want to get a specific value in the array without using select 0 of foreach

Share this post


Link to post
Share on other sites

I have no experience in PHP and a cracking hangover, so this may not be the best method... But it works POORLY [Edit].
 

Spoiler

// I want to find the number value of "BBB"!
_array = [["AAA",111],["BBB",222],["CCC",333]]; 
_return = _array select { _x#0 isEqualTo "BBB" };   // Return is [["BBB",222]].

_return #0#1;   // Return is 222.

 


Hopefully that's some use to you.

Ignore this, please!

Share this post


Link to post
Share on other sites
2 minutes ago, Maff said:

// I want to find the number value of "BBB"!

_array = [["AAA",111],["BBB",222],["CCC",333]];

_return = _array select { _x#0 isEqualTo "BBB" }; // Return is [["BBB",222]].

 

_return #0#1; // Return is 222.

oof.

 

Yours iterates through all elements of the array even if the wanted value was already found.

_index = _array findIf { _x#0 isEqualTo "BBB"};

_array select _index

 

  • Like 2

Share this post


Link to post
Share on other sites
1 minute ago, Dedmen said:

oof.


Damnit!

Using findIf was my first thought but my brain is... Working not good.

Nice one!

Share this post


Link to post
Share on other sites

Thank you for your answers.

 

the smallest function i have ever written xD

 

findIf I didn't know.

 

FNC_PUBLIC_ARRRETURNVAR = {
	params ["_arr","_var"];
	_return = (_arr select (_arr findIf { _x#0 isEqualTo _var})) select 1;
	_return
};

 

_array = [["AAA",111],["BBB",222],["CCC",333]]; 

_var = [_array,"BBB"]call FNC_PUBLIC_ARRRETURNVAR;

 

I am currently rewriting my entire mission and would like to optimize as much as possible.

 

Share this post


Link to post
Share on other sites

What you are doing looks alot like CBA hash.
but cba does the layout like so:
[[AAA, BBB, CCC], [111,222,333]]
then you do
(_array select 1) select ((_array select 0) find "AAA")

which is way more efficient as find does the searching completely in engine without having to run slow scripts.

  • Like 1

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

×