Jump to content

Recommended Posts

Hi.

I have a question on arrays.

//I get an output as array eg :

_fruitarray =  [["apple",1],["banana",4],["pear",32]]

//I want to convert it to:

["apple","banana","pear"]

//What would be the correct syntaxis for that ?

//I tried:

{_x select 0} foreach _fruitarray

//but it only shows one of the elements, and I need an array to be returned.

Thanx

Share this post


Link to post
Share on other sites
_newArray = [];
_fruitarray =  [["apple",1],["banana",4],["pear",32]];
{
	_newArray pushBack (_x select 0);
} foreach _fruitarray;

I didn't test and i literally just woke up so no guarantees :).

_newArray should contain apple, banana, ...

Share this post


Link to post
Share on other sites
3 hours ago, stanhope said:

_newArray = [];
_fruitarray =  [["apple",1],["banana",4],["pear",32]];
{
	_newArray pushBack (_x select 0);
} foreach _fruitarray;

I didn't test and i literally just woke up so no guarantees :).

_newArray should contain apple, banana, ...

If I copypaste your code into the console, the output is 2. Don`t quite understand how that happens. (Select 0 gives 2, select 1 gives 2, select 2 gives empty, select 3 and up - zero divisior)

Share this post


Link to post
Share on other sites

What output are you talking about?  

Share this post


Link to post
Share on other sites
On 15.4.2018 at 1:24 PM, stanhope said:

What output are you talking about?  

He means that it returns 2. Which is the return value of the forEach which is the return value of the last pushBack.
Why are you doing that so complicated?

_fruitarray apply {_x select 0}

Done.

  • Thanks 1

Share this post


Link to post
Share on other sites
26 minutes ago, Dedmen said:

He means that it returns 2. Which is the return value of the forEach which is the return value of the last pushBack.

I figured as much yes, but wanted to be sure.

 

27 minutes ago, Dedmen said:

Why are you doing that so complicated?


_fruitarray apply {_x select 0}

Done.

Because I didn't know about that command :)

Share this post


Link to post
Share on other sites
On 4/16/2018 at 12:37 PM, Dedmen said:

He means that it returns 2. Which is the return value of the forEach which is the return value of the last pushBack.
Why are you doing that so complicated?


_fruitarray apply {_x select 0}

Done.

Yup, that does the job !

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

×